
Python classmethod - Get the class name from the class or instance
The Python class method is declared by @classmethod decorator.
class Account:
def __init__(self, name):
self.name = name
@classmethod
def get_class_data(cls):
print('This class is ' + cls.__name__ + '.')
Account.get_class_data() # This class is Account.
a = Account(name='Kyle')
a.get_class_data() # This class is Account.
The class method can be called the class itself and instance. The first argument of the class method is usually named cls
. Most of its properties are the class information including the name, doc, etc.
Comments
Powered by Markdown