-2

In the below code snippet, no classmethod or staticmethod is used but class property can be altered just by defining normal methods inside a class, then what's the point? Am I missing something?

Can anyone please explain this behavior?

class test:
    var = 10
    def changeVariable():
        test.var = 100
    def reset():
        test.var = 10
>>> test.var
10
>>> test.changeVariable()
>>> test.var
100
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Kazee
  • 31
  • 3
  • Here is a good explanation. [Python's Instance, Class, and Static Methods Demystified](https://realpython.com/instance-class-and-static-methods-demystified/) – Sun Bear May 21 '21 at 17:45

1 Answers1

0

Methods in a class are just functions. Function objects are non-data descriptors in python. That means that when you look up a method in an instance, it will be accessed from the class.

Your example calls a bunch of functions. You can check this by doing

>>> type(test.reset)

The functions assign the var attribute of an object called test. The fact that test is a class is totally incidental here: you're just using it as a regular namespace.

To see what happens when you try to bind one of these functions to an instance as a method, you need to create an instance:

>>> inst = test()

Now you can check that your functions are behaving as methods:

>>> type(inst.reset)

You'll get an error if you try to call inst.reset() though, because a bound method is a closure that passes the instance as a first implicit parameter:

>>> inst.reset()

As you correctly noted, the workaround is to either change the function signature, or wrap it in a staticmethod decorator.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Agree with your point , I was just wondering if i can just use the class name in the same class to access its attributes and modify the changes at class level when why do i have @classmethods ,it does not make any sense to add another overhead of creating a decorator . Here i am ruling out the class instantiation , my concern is only with classmethods and static methods. – Kazee May 22 '21 at 07:54
  • @user15994405. You can't use the class name in the class body, and you're not. You're using the class name in a function object that happens to live in the class namespace. The class name is not accessed until you call the function. I'm not really sure why you're using a class as a namespace though, given that you don't want to use any of the descriptor handling functionality it provides. – Mad Physicist May 22 '21 at 09:05
  • thanks for the info , to summarize basically we can use methods the way i posted to achieve the same functionality as an hack but the standard and better way is using a staticmethod and classmethod . correct me if i am wrong – Kazee May 22 '21 at 16:07
  • @Kazee. A better way is just to make a container of functions. If you're not using something for its intended purpose, you could probably do it better. At the same time, if your way works, it's fine too – Mad Physicist May 22 '21 at 17:23