Class methods
1. Class methods
We looked at class attributes, now let's look at class methods!2. Methods
We've already seen how methods created in a class can be used by any object of that class. If we have two Employee objects with different values for their name and salary attributes, calling the give_raise method on each object separately results in different salaries between objects. In other words, the methods are using instance-level data.3. Class methods
However, it is possible to define methods bound to a class rather than an object. These class methods must have a narrow scope because they will not be able to use any object-level data. To define a class method, we start with a classmethod decorator. Remember, a decorator is convenience feature that allows us to modify the behavior of a function. Also remember that a method is a function that is specific to a class. So, the classmethod decorator allows us to modify the behavior of the method defined directly afterwards. When defining the class method, the only difference compared to previous methods is that now the first argument is not self, but cls, referring to the class. This is just like how the self argument was a reference to a particular object. Then we write it as any other function, keeping in mind that we can't refer to any instance attributes in that method. To call a class method, we use class-dot-method syntax, rather than object-dot-method syntax. Note that as with self, cls is used by convention but any word will work.4. Alternative constructors
So why would we ever need class methods? The main use case is to enable alternative constructors. A class can only have one init method, but there might be multiple ways to initialize an object. For example, we might want to create an Employee object from data stored in a file. We can't use a method, because it would require an instance, and there isn't one yet! Here, we introduce a class method from_file that accepts a file name, reads the file that contains the name of the employee in the first line and the salary in the second line, returning an object. In the return statement, we use the cls variable. Remember that cls refers to the class, so this will call the init constructor, just like using Employee with parentheses would when used outside the class definition.5. Alternative constructors
Then we can call Employee.from_file by using class-dot-method syntax, which will create an employee object without explicitly calling the constructor. The file only contains the name and the salary, so these values are assigned to the emp object's attributes.6. When to use class methods
Given class methods require a narrow scope, when should we use them? We've seen their application as an alternative constructor, such as creating an object from a file or accepting a different format than a regular constructor. Remember, class methods can't access instance attributes. While this might seem like a limitation, it means we can create methods that don't require these attributes to work correctly! Another scenario is when we want to restrict a class to a single object. This is known as the Singleton design pattern and can be useful when exactly one instance of a class is needed to control access to resources, such as database connections or configuration settings.7. Let's practice!
Your turn to build class methods.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.