If I define:
class Bar(object):
@staticmethod
def bar():
# code
pass
class Foo(Bar):
# code
pass
Is it possible for a function call Foo.bar() to determine the class name Foo?
If I define:
class Bar(object):
@staticmethod
def bar():
# code
pass
class Foo(Bar):
# code
pass
Is it possible for a function call Foo.bar() to determine the class name Foo?
If you need to find the class information, the appropriate way is to use
@classmethod
.Now your
bar
method has a reference to the class ascls
which is the actual class of the caller. And as shown in the code,cls.__name__
is the name of the class you are looking for.Replace the staticmethod with a classmethod. This will be passed the class when it is called, so you can get the class name from that.