Summary: in this tutorial, you’ll learn about Python class methods and when to use them appropriately.
Introduction to Python class methods #
So far, you learned about instance methods that are bound to a specific instance of a class.
Instance methods can access instance variables within the same class. To invoke instance methods, you need to create an instance of the class first.
The following defines the Person class:
class Person:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def get_full_name(self):
return f"{self.first_name} {self.last_name}"
def introduce(self):
return f"Hi. I'm {self.first_name} {self.last_name}. I'm {self.age} years old."Code language: Python (python)The Person class has three instance methods including __init__(), get_full_name(), and introduce().
Suppose that you want to add a method that creates an anonymous person to the Person class.
In order to do so, you would come up with the following code:
class Person:
# ... other methods
def create_anonymous(self):
return Person('John', 'Doe', 25)
Code language: Python (python)The create_anonymous() is an instance method that returns an anonymous person.
However, to invoke the create_anonymous() method, you need to create an instance, which doesn’t make sense in this case.
This is why Python class methods come into play.
A class method isn’t bound to any specific instance. It’s bound to the class only.
To define a class method:
- First place the
@classmethoddecorator above the method definition. For now, you just need to understand that the@classmethoddecorator will change an instance method to a class method. - Second, rename the
selfparameter tocls. Theclsmeansclass. However,classis a keyword so you cannot use it as a parameter.
The following shows the new version of the Person class:
class Person:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def get_full_name(self):
return f"{self.first_name} {self.last_name}"
def introduce(self):
return f"Hi. I'm {self.first_name} {self.last_name}. I'm {self.age} years old."
@classmethod
def create_anonymous(cls):
return Person('John', 'Doe', 25)
Code language: Python (python)The create_anonymous() method cannot access instance attributes. But it can access class attributes via the cls variable.
Calling Python class methods #
To call a class method, you use the class name, followed by a dot, and then the method name like this:
ClassName.method_name()Code language: Python (python)The following example shows how to call the create_anonymous() class method of the Person class:
anonymous = Person.create_anonymous()
print(anonymous.introduce())
Code language: Python (python)Output:
Hi. I'm John Doe. I'm 25 years old.Code language: Python (python)Class methods vs. instance methods #
The following table illustrates the differences between class methods and instance methods:
| Features | class methods | Instance methods |
|---|---|---|
| Binding | Class | An instance of the class |
| Calling | Class.method() | object.method() |
| Accessing | Class attributes | Instance & class attributes |
When to use Python class methods #
You can use class methods for any methods that are not bound to a specific instance but the class. In practice, you often use class methods for methods that create an instance of the class.
When a method creates an instance of the class and returns it, the method is called a factory method. For example, the create_anonymous() is a factory method because it returns a new instance of the Person class.
Summary #
- Python class methods aren’t bound to any specific instance, but classes.
- Use
@classmethoddecorator to change an instance method to a class method. Also, pass theclsas the first parameter to the class method. - Use class methods for factory methods.