class A:
def foo(self):
print "foo()"
getattr(A, foo) # True
A.foo() # error
getattr(A(), foo) # True
A().foo() # prints "foo()"
That being said, here is my problem:
I wish to store test case meta information as attributes of the Test Case class objects themselves, not on instances of them.
I have a list of attribute names to extract, but if there is an instance method of the same name, then getattr(class_obj, attr)
will return True, but getattr(class_obj, attr)()
raises an Error.
Is there a way to tell getattr not to include attributes of the instantiated class and only of the class object itself?
EDIT: I tried accessing class_obj.__dict__
directly (which I understand is bad practice), but it does not include some attributes like __name__
EDIT: Rephrase of the question. Is there a way to differentiate between methods of the class obj and the methods of an instance of the class?
foo()
is a (non-static) method, thus an attribute namedfoo
will be both on the class and on its instances. The reason whyA.foo()
raises an error is because you can't call an instance method on a class object, but it's still there as an unbound method. I think you might be confusing attributes and methods a little. "Does not have an attribute" and "the attribute isn't callable" are different things. – PositivismA.foo()
, catch the error withtry..except
, if an error occurs just pretend the attribute wasn't there. – Positivismself
parameter? (Practically, it would still require any other parameters though.) – Positivism