Differences between obj.attribute and getattr(obj, "attribute")

Chris Angelico rosuav at gmail.com
Wed Dec 11 10:38:43 CET 2013

On Wed, Dec 11, 2013 at 8:30 PM, Jurko Gospodnetić
<jurko.gospodnetic at pke.hr> wrote:
>   Also, you can not test whether an object has an attribute when using the
> object.attribute access method without raising/catching an exception and
> then it can be hard to make sure no other code caused the exception.

It's pretty easy to make sure no other code caused the exception -
just have no other code inside the try block.

x.foo(input().split(" ")[4])  # Might throw from any point in the expression

   func = x.foo
except AttributeError:


More information about the Python-list mailing list