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

Chris Angelico rosuav at gmail.com
Wed Dec 11 04:38:43 EST 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

try:
   func = x.foo
except AttributeError:
   deal_with_error
func(......)

ChrisA



More information about the Python-list mailing list