Edit: I am not asking about what are classmethod and staticmethod or difference between them. Just asking the question to clarify what does state of class mean.
I just started python. While going through tutorials on @classmethod
and @staticmethod
I found a statement similar to below one in multiple websites.
Mentioned in geekforgeeks
A class method can access or modify class state while a static method can’t access or modify it.
Class method can access and modify the class state. Static Method cannot access or modify the class state.
What does the class state mean? Does it mean that there is a way of modifying the values of all objects at go by using class method,because when a class state change it should affect all the objects created from that class? I could find only factory method creation with @classmethods and I don't think it is some class state change.
I am an advanced C++ programmer. Some related explanation would be good , if possible.
Edit: The question which marked this as duplicate of it doesn't mention the class states. I read both that questions and its duplicate before asking this.
One example I tried:
class MyClass:
myvar = 100
def __init__(self,age):
self.myvar = age
def instMethod(self):
print("Inst method")
@classmethod
def classMethod(cls,age):
cls.myvar = age
obj1 = MyClass(10)
obj2 = MyClass(20)
obj3 = MyClass(30)
print(obj1.myvar)
print(obj2.myvar)
print(obj3.myvar)
print("after class method")
MyClass.classMethod(45)
print(obj1.myvar)
print(obj2.myvar)
print(obj3.myvar)
output:
10
20
30
after class method
10
20
30
But my expectation was
10
20
30
after class method
45
45
45