2
When you use []
notation, internally, Python will try to invoke __getitem__
method on that object. Since the Organism
object didn’t define that method, it fails with that error.
Instead, you can use getattr
function, like this
print getattr(o, f)
You can even specify a default value to be used if the attribute is not present in the object, like this
print getattr(o, f, "{} not present in {}".format(f, o))
Source:stackexchange.com