'Using classmethod vs. using class variable directly

I have an example class:

class A():
  x = 10
  y = 10
  
  @classmethod
  def add_to_y(cls):
    cls.y += 1
    
    
    
first, second = A(), A()

print(first.x, second.x)
A.x +=1
print(first.x, second.x)
second.add_to_y()
print(first.y, second.y)

Which returns:

10 10
11 11
11 11

Both ways increment class variable x or y. Is there a difference? Which one is the recommended way of incrementing a class variable?

Trying to understand when should I use one instead of the other, and I just think that using classmethod is kind of convenient to using inside methods that might do more than just one thing, or am I missing something?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source