Skip to main content
Recover effbot.org reference with the last relevant version captured by the Wayback Machine
Source Link
getattr(my_object, my_str)

Or, if you're not sure if the name exists as a key and want to provide a fallback instead of throwing an exception:

getattr(my_object, my_str, "Could not find anything")

More on getattr.More on getattr.

getattr(my_object, my_str)

Or, if you're not sure if the name exists as a key and want to provide a fallback instead of throwing an exception:

getattr(my_object, my_str, "Could not find anything")

More on getattr.

getattr(my_object, my_str)

Or, if you're not sure if the name exists as a key and want to provide a fallback instead of throwing an exception:

getattr(my_object, my_str, "Could not find anything")

More on getattr.

Added info about default option for clarity
Source Link
Tom
  • 22.9k
  • 5
  • 67
  • 97
getattr(my_object, my_str)

Or, if you're not sure if the name exists as a key and want to provide a fallback instead of throwing an exception:

getattr(my_object, my_str, "Could not find anything")

More on getattr.

getattr(my_object, my_str)

More on getattr.

getattr(my_object, my_str)

Or, if you're not sure if the name exists as a key and want to provide a fallback instead of throwing an exception:

getattr(my_object, my_str, "Could not find anything")

More on getattr.

Source Link
Tom
  • 22.9k
  • 5
  • 67
  • 97

getattr(my_object, my_str)

More on getattr.