7.12 Class and static methods
Class methods and static methods are both defined within a class. Unlike an instance method, Python does not bind a class method to a particular instance. It is bound to and knows about the entire class. A static method is a function that sits within a class but knows nothing special about the class or its instances.
You create a class method by placing the @classmethod
decorator on the line before the method’s definition.
Instead of using self
for the first parameter as you would in an instance method,
use “cls
”. At runtime, Python passes the class object to the method as the first
argument.
@classmethod
def get_count_class(cls):
return cls.number_of_guitars
If you refer to a class variable within a class method, prefix the variable name with
cls
and a “.
”. Do not hardcode the name of the class into the call,
as...