Static and Non Static Method in Python | Class Method in Python
Static and Non Static Method in Python | Class Methods in Python
Methods:
- Methods are used to define a business logic
- There are three types of methods in python
B. Class Method
C. Static Method
- Non-static methods are object level
- Non-static method should have a first argument (Self)
- Note:- We can give any other argument name instead of self
- Class methods are class level
- Use to have all the class level variable
- It should have decorator @Class method
- Must and should have one argument (cls)
Note:- We can give another value name instead of cls
- A static method is used to define small utility actions
- Addition of a number
- Subtraction of a number etc.
- · It should have decorator @Static method
Example:
Question: Where we can
call the static variable?
Answer: We can call under constructor,
non-static method, class method, and static method as well.
A static variable can be called inside
a constructor, non-static method, class method, and static method using the class
name.
- It is possible to change the static variable values inside a constructor
- It is possible to change the static variable values inside a non-static method
- Similarly, we can change the static variable value inside a non-static method, class method, and static method.
· Non-static variables cannot access inside a class method or static method using self.
|
Constructor |
Method |
|
Identifier as __init__ |
Any valid identifier
name |
|
Never returns values |
It will return a value
(Single or multiple) |
| Automatically executed (When creating an object) |
Should be called
explicitly |
|
Object-level |
Object and class level |
Comments
Post a Comment