How to Use Class and Static Methods in Python

TL;DR
Use a class method when an operation needs the class itself, and use a static method when the operation needs neither the class nor an instance. Class methods receive cls automatically and can update class variables or serve as alternative constructors, while static methods behave like regular functions placed inside a class because their purpose is logically connected to it.
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 named self. A class method automatically receives the class instead, conventionally named cls, after being decorated with @classmethod. A static method receives neither the instance nor the class automatically. It behaves like a normal function but is placed inside the class because its operation has a logical connection to that class.
Q: How do you create a class method in Python?
Create a class method by placing the @classmethod decorator directly above the method definition. Its first parameter should conventionally be named cls, which represents the class supplied automatically when the method is called. Additional parameters follow cls. The tutorial's set_raise_amount method accepts cls and an amount, then assigns that amount to the raise_amount class variable through cls.raise_amount.
Q: Why is cls used instead of class in a class method?
The name cls is a common convention for the first parameter of a class method because that parameter receives the class automatically. The word class cannot be used as the variable name in this position because it is a Python keyword with a separate language-level purpose. It appears in the syntax used to create a new class, so the shortened name cls avoids that conflict.
Q: How can a class method update a shared class variable?
A class method can update a shared class variable by assigning a new value through its cls parameter. In the example, set_raise_amount receives an amount and assigns it to cls.raise_amount. Calling Employee.set_raise_amount with 5% therefore has the same effect as assigning 5% directly to Employee.raise_amount, and the demonstrated class and employee instances subsequently report that updated value.
Q: Can you call a Python class method from an instance?
Yes, the tutorial demonstrates that a class method can be called from an instance as well as directly from the class. Calling set_raise_amount through an employee instance still operates on the class variable and changes the value reported by the class and both demonstrated instances. However, the tutorial also says that calling a class method this way does not make much sense and is not commonly seen.
Q: How do class methods work as alternative constructors?
A class method works as an alternative constructor by accepting data in a different representation, converting that data into the values required by the normal initializer, creating an object through cls, and returning the result. The from_string example accepts a hyphen-separated employee string, splits it into first name, last name, and pay, constructs the employee, and returns the new employee object.
Q: Why use an alternative constructor for parsed employee strings?
An alternative constructor prevents every caller from repeating the same parsing steps before creating an object. In the tutorial's use case, employee information repeatedly arrives as hyphen-separated strings containing a first name, last name, and salary. The from_string class method centralizes the split operation and object creation, allowing callers to pass the original string directly and receive a completed employee object.
Q: When should you use a static method in a Python class?
Use a static method when a function has a logical connection to a class but does not need either a particular instance or the class itself. A static method takes only the arguments its operation actually requires. The tutorial uses an is_workday example that accepts a day because checking that supplied date is related to employees, yet does not depend on instance data or class variables.
Summary & Key Takeaways
-
Regular methods, class methods, and static methods differ mainly in what Python supplies automatically. A regular method receives the instance as self, while a class method receives the class as cls. A static method receives neither one automatically and accepts only the arguments explicitly required for its work.
-
The @classmethod decorator converts a regular method into one that operates on the class. The tutorial demonstrates this with set_raise_amount, which changes the shared raise_amount class variable. Calling the method with 5% causes the class and both demonstrated employee instances to report the updated 5% value.
-
Class methods can provide alternative ways to construct objects. The from_string method accepts a hyphen-separated employee string, splits it into first name, last name, and pay, creates an employee through cls, and returns it. Static methods instead suit related utilities, such as checking whether a supplied date is a workday.
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