I am stumped by something that should be easy. I am trying to determine if a class has a specific property using hasattr, but it is always returning False. Manually trying to getattr yields a strange exception.
class Test(object):
@classmethod
def create(cls, **kwargs):
newCls = cls()
if kwargs:
for arg, value in kwargs.iteritems():
if hasattr(newCls, arg): # always returns False
print 'Found property:', arg
if arg in dir(newCls): # this works
print 'Found property in dir', arg
if hasattr(newCls, 'myProperty'): # hardcoding the string also works
print 'myProperty exists on', newCls
try: # this does not work, raises a list index out of range error....?
getattr(newCls, arg)
except Exception as e:
print e.args[0] # returns: list index out of range
return newCls
I am stumped…
EDIT: My problem is a syntax error with the property itself, nothing to do with hasattr.