mercredi 20 août 2014

[Python] How to check if an object has an attribute : hasattr vs __dict__

class A(object):
   foo = 1

class B(A):
   pass


b = B()
print( hasattr(b, 'foo') )
True

print( 'foo' in b.__dict__)
False


hasattr : checks on super class
__dict__ : does not include super class

Aucun commentaire:

Categories