When a static method is called, is there any way for it to know which subclass it was invoked from?
(I’m aware that this is very un-OO and probably never useful in a well written program, but I’d like to know if the language provides it)
For example:
class A(object):
@staticmethod
def foo():
print 'bar'
# *** I would like to print either 'A' or 'B' here
class B(A):
pass
A.foo()
B.foo()
You have to use a
@classmethodinstead of a@staticmethodfor this. With a class method, you get a reference to the class passed in as first argument:Output: http://codepad.org/bW3E51r9