1

I definitely understand the difference between instance methods and class/static methods in Python. Sometimes you don't want an actual instance of an object to perform some operations. Maybe it's getting certain information that's stored across all instances. However, I can not for the life of me understand the difference between class and static methods.

I understand that class methods take a class argument (usually cls) and that refers to the current class. Then static methods don't need to take any arguments. However, the only main difference I've seen online is that class methods have access to read/change "class states". I cannot find a good explanation on what a "class state" is.

Here is some code I ran with the output below.

class Person:
    SPECIES = "homo sapien"

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f"{self.name} is {self.age} years old"

    def get_species(self):
        print(f"species: {Person.SPECIES}")

    def change_species_instance_method(self):
        Person.SPECIES="homo sapien (instance)"

    @classmethod
    def change_species_class_method(cls):
        cls.SPECIES="homo sapien (class)"

    @staticmethod
    def change_species_static_method():
        Person.SPECIES="homo sapien (static)"

me = Person("Kevin", 20)
sam = Person("Sam", 20)

me.get_species()
sam.get_species()

me.change_species_instance_method()
print()

me.get_species()
sam.get_species()

me.change_species_class_method()
print()

me.get_species()
sam.get_species()

me.change_species_static_method()
print()

me.get_species()
sam.get_species()

OUTPUT

species: homo sapien
species: homo sapien

species: homo sapien (instance)
species: homo sapien (instance)

species: homo sapien (class)
species: homo sapien (class)

species: homo sapien (static)
species: homo sapien (static)

So, if both class and static methods can modify class variables (SPECIES in this case), then what is the difference between them, apart from the definition of class methods requiring cls? Thank you!

Also, another difference people have said is that class methods are used to create new instances of the class, but I've been able to do that with static too. So for something in the relative complexity of the code I'm writing (i.e. so no class inheritance) is there any functional difference between static and class methods?

Kevin
  • 121
  • 8
  • 3
    Does this answer your question? [Difference between staticmethod and classmethod](https://stackoverflow.com/questions/136097/difference-between-staticmethod-and-classmethod) – felipe May 01 '20 at 18:27
  • I don't know why you closed my question @prune . The other post you linked does not answer my question at all. I asked specific questions here which are not addressed in the other post. – Kevin May 01 '20 at 18:40
  • Okay ... *I* see how the generalities apply, but if they aren't at the level you need, As such, I have no problem reopening. – Prune May 01 '20 at 18:55
  • thanks :) yeah I understand at quick glance it seems like the same, and that's sort of my own frustration is every question/answer seems to be very similar but doesn't answer a key part of this. Thanks – Kevin May 01 '20 at 18:57
  • 1
    Static methods are literally just methods which do not accept any implicit first argument, neither `self` nor `cls`. That makes them just normal functions attached to a class. That alone makes them typically suspect. The only real use I’ve found for them is when overriding parent methods so you need to implement a defined interface, but you have no use for `self` nor `cls` and want to make that explicit. Some style checkers mark unused arguments as superfluous. – deceze May 01 '20 at 19:07
  • "then what is the difference between them, apart from the definition of class methods requiring cls" There is none. Honestly, I'm having a hard time understanding what you question is, because you seem to already understand the difference. When you speak of the *state* of an object (like a class object) it pretty much means the *attributes* of that object. With a `@classmethod`, you have access to the attributes of that class, the class state, because it is implicitly passed the class. Static methods are not really something that you see used a lot in Python. – juanpa.arrivillaga May 01 '20 at 19:12
  • Hey @juanpa.arrivillaga I guess then I can ask one simple question. I've seen in several places "static methods do not have access to class attributes, but class methods do", but the code I ran above shows that's incorrect, right? The change_species_static_method is a static method that changed a class attribute for all instances of a class. – Kevin May 01 '20 at 19:21
  • Sure, but only by referring to the class object in the global scope directly. You can do that *without any method at all*, and just a function in the module scope (which is almost always what a static method should be). You could also just do that with an instance method. – juanpa.arrivillaga May 01 '20 at 19:38
  • A big difference comes into play with inheritance. A class method will receive its child `cls` as argument, a static method won’t. – deceze May 01 '20 at 19:41

0 Answers0