Python OOP Tutorial 3: What Is the Difference Between Classmethods and Staticmethods?

TL;DR
Use a class method when an operation needs the class, and use a static method when it needs neither the class nor an instance. The @classmethod decorator supplies cls automatically, enabling shared variable updates and alternative constructors, while a static method accepts only its required arguments. Read on for the 5% raise example, from_string constructor, and is_workday utility.
Transcript
hey everybody how's it going in our last video we looked at the difference between instance variables and class variables and in this video we'll be learning about the difference between regular methods class methods and static methods and a lot of people get confused as to the difference between class methods and static methods so we'll definitely... Read More
Key Insights
- A regular instance method automatically receives the current instance as its first argument. Python programmers conventionally name that parameter self, allowing the method to read or modify data associated with the particular object on which the method was called.
- A class method automatically receives the class as its first argument instead of receiving an instance. The conventional parameter name is cls because class is already a Python keyword used when defining a new class.
- The @classmethod decorator changes a method so it works with the class. In the employee example, set_raise_amount assigns the supplied amount to cls.raise_amount, producing the same class-variable change as assigning the amount directly through Employee.raise_amount.
- A shared class variable can affect values accessed through both the class and its instances. After set_raise_amount is called with 5%, the tutorial shows the class and both existing employee instances reporting 5% for the raise amount.
- A class method can be called from an instance as well as from the class. The example shows that an instance call still changes the shared class variable, although the tutorial notes that calling class methods through instances is not a commonly seen approach.
- An alternative constructor is a class method that provides another way to create an object. The from_string method parses a hyphen-separated employee string, passes the resulting values to cls, and returns the newly created employee object.
- The from prefix is a convention for naming alternative constructors. The tutorial uses from_string for employee data and points to from_timestamp in the datetime module as a real-world example of accepting another representation, parsing it, and returning a new object.
- A static method receives neither self nor cls automatically. It behaves like a regular function but belongs inside a class when its operation has a logical connection to that class, such as determining whether a supplied date is a workday for an employee-related class.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: What is the difference between regular, class, and static methods in Python?
A regular method automatically receives an instance as its first argument, conventionally called self. A class method receives the class as cls, while a static method receives neither the instance nor the class automatically.
Q: How do you create a class method in Python?
Place the @classmethod decorator above the method definition and use cls as its first parameter. Python then supplies the class automatically when the method is called.
Q: Why do Python class methods use cls instead of class?
The first parameter is conventionally named cls because it receives the class automatically. The name class cannot be used because class is a Python keyword used to create a new class.
Q: How can a class method update a shared class variable?
The method assigns the new value through its cls parameter, as set_raise_amount does with cls.raise_amount. Passing 5% updates the value reported by the Employee class and both demonstrated employee instances.
Q: Can a Python class method be called from an instance?
Yes, a class method can be called through an instance and still operate on the class. The tutorial demonstrates that this changes the shared raise amount for the class and both instances, although it says this approach is not commonly seen.
Q: How do class methods work as alternative constructors?
A class method can accept data in another representation, parse it, create an object through cls, and return the new object. The from_string method splits a hyphen-separated employee string into first name, last name, and pay before constructing the employee.
Q: Why use an alternative constructor for employee strings?
It keeps callers from repeating the same parsing process whenever employee data arrives as a hyphen-separated string. Callers can pass the original string to from_string and receive a completed employee object.
Q: When should you use a static method in a Python class?
Use a static method when an operation is logically connected to the class but does not need an instance or the class itself. The is_workday example accepts a supplied date because the check relates to employees without using instance data or class variables.
Summary & Key Takeaways
-
Definition: A regular method receives the current instance automatically as its first argument, conventionally named self.
-
Definition: A class method receives the class automatically as its first argument, conventionally named cls.
-
Tool: The @classmethod decorator converts a regular method into a method that operates on the class.
-
Number: The original raise_amount class variable is 4%.
-
Number: Passing 5% to set_raise_amount updates the class and both demonstrated instances to 5%.
-
Compare: Calling set_raise_amount has the same effect as assigning the value directly to Employee.raise_amount.
-
Definition: An alternative constructor provides another way to create an object from a different data representation.
-
Step 1: from_string accepts an employee string containing a first name, last name, and pay separated by hyphens.
-
Step 2: from_string splits the string and passes the parsed values through cls to create the employee.
-
Definition: A static method receives neither self nor cls and accepts only the arguments required for its operation.
-
Tool: is_workday is a static-method example because checking a supplied date is related to employees but needs no instance or class data.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from Corey Schafer 📚






Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator