- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Python class method vs. static method
Class methods are methods that have a reference to a class object as their first argument, whereas static methods are methods that may have no parameters.
Static methods do not have access to what the class is. The static method is only a function without access to the object or its internals.
Class methods have access to what the class is. The class method is also a function, but with access to the object and its internals like fields and other methods.
In Python, there is a distinction between class methods and static methods
Before distinguishing between the two methods, class method and static method, let's first create a class with a normal method, a class method, and a static method:
class fresherearth: def myfun(self, x): print("First parameter (self):", self) print("Second parameter (x):", x) @classmethod def classfun(cls, x): print("First parameter (cls):", cls) print("Second parameter (x):", x) @staticmethod def statfun(x): print("First parameter (x):", x)
Note: The @classmethod decorator is used to define class methods, whereas the @staticmethod decorator is used to define static methods.
To make an object of the class "fresherearth," say "ob," here is the code:
ob = fresherearth()
Object "ob" has now been created. If we print the "ob" using the statement:
print(ob)
The output looks like this:
<__main__.fresherearth object at 0x00000255CF48DF10>
Here is the code to call a method, say myfun(), using the above-created object, that is, ob. I've passed the string "Hello" as an argument.
ob.myfun("Hello")
The object instance "ob" will be passed as the first argument implicitly. Therefore, "ob" refers to "self." And "Hello" gets passed as a second argument. Therefore, "Hello" refers to "x." Here is the output produced by calling the function myfun() using the above code:
First parameter (self): <__main__.fresherearth object at 0x0000019C1B03DF10> Second parameter (x): Hello
Let's use the same object "ob" to invoke the class method classfun(). In the case of class methods, the class of the object instance is passed as the first argument implicitly. For example, consider the following statement:
ob.classfun("Hello")
where "ob" is an object of the class named "fresherearth." Therefore, the class "fresherearth" will get implicitly passed as the first argument to classfun(). That is, fresherearth refers to "cls" (the first argument of classfun()), whereas "Hello" refers to "x" (the second argument of classfun()). Here is the output produced by the above code:
First parameter (cls): <class '__main__.fresherearth'> Second parameter (x): Hello
Also, the class method named classfun() can also be called directly using the class itself. And of course, most programmers define class methods so that they can call those class methods without creating an instance of the class. Here is the code to call the class method named classfun() without an instance of the class:
fresherearth.classfun("Hello")
That is, there is no need to create an object for the class. Just call the class method using the similar code given above. The output should be the same as the previous one, that is:
First parameter (cls): <class '__main__.fresherearth'> Second parameter (x): Hello
The static method can also be called either directly using the class itself or using the instance of the class. That means it is similar to a class method, but static methods are like normal functions. There is no need for the "self" and "cls" parameters.
Note: The self is an instance of an object, while the cls is the class.
Here is the code to call static methods. Call the static method either using the object instance of the class, that is:
ob.statfun("Hello")
Or by directly calling the method with the help of a class in this way:
fresherearth.statfun("Hello")
The output should be the same in both cases, which is:
First parameter (x): Hello
« Previous Tutorial Next Tutorial »