Class and static methods

There are several types of methods available for classes. Instance methods are what we have been talking about so far. Class and static methods are other ways to create Python methods.

Class methods are called on the class itself, not just a particular instance of the class. Static methods apply to all instances of a class, not just a specific one.

An example of their use is contained in the following screenshot:

Class and static methods (part 1)

A class about dogs is made on line 10. The initialization method takes arguments for the dog's breed and age. Getter methods are written to return the breed and age, but aren't really necessary because the last method in the class, __repr__(), defines the print formatting used when asking for those values. These getter methods could be changed to Python properties as well.

The first new thing we encounter is the @staticmethod decorator. The method defined here can be applied to all instances of the class, as well as the class itself; note that there is no self within the parentheses. They are restricted in what data they can access, and they cannot modify the state of an instance or class.

Next, we have @classmethod. The method defined here can only be used when calling the class itself; it cannot modify instances. This is because the argument passed in the parentheses is cls, rather than self. Thus, class methods only know about the class and are oblivious to any instances of the class. For this class method, it checks to see whether the class that called it is Dog, or some other class, such as a subclass that inherits this method.

We make an instance of this class on line 11, and then print the breed and age of the dog on lines 12 and 13. If we attempt to do the same with the class itself, the only information we get is that there is an address location for Dog.breed but an error is generated when age is requested. This is because the class acts as a container for instances, but cannot access normal instance methods.

The program is continued in following screenshot:

 Class and static methods (part 2)

Lines 26 and 27 demonstrate accessing the static method. Because there is no self or cls argument for a static method, it can be used with anything related to the class. In other words, it can be used with any instance of the class, as well as the class itself and any subclasses.

Speaking of subclasses, we make one on line 28. All methods are inherited and we don't need any new ones, so pass is used to tell Python to continue. An instance of the subclass is created on line 29.

Lines 30 and 31 demonstrate use of the class method. As stated earlier, when the class method is invoked on the base class Dog on line 30, it returns a predefined string. But if called on a subclass (line 31), it returns the name of the subclass.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.210.91