Given I am inside a @staticmethod, how can I find the name of the current class ?
As i don't have access to self.__class__.__name__
Given I am inside a @staticmethod, how can I find the name of the current class ?
As i don't have access to self.__class__.__name__
As seen in this answer How to get (sub)class name from a static method in Python?
you can change the @staticmethod
to a @classmethod
which takes the class as a parameter:
class Bar(object):
@classmethod
def bar(cls):
print cls.__name__
....
If you have control of the method (e.g. it's one you wrote), the best way is to use the @classmethod
decorator instead. That will send the class as the first argument:
class Spam(object):
@classmethod
def eggs(cls):
return cls.__name__