+ 1

Static methods, classes and methods, yaeh, and objects in Python

Habits die hard, I don't understand why I can directly access the MyClass2 method if I don't create an object first? How come nothing happens if I use or don't use the @staticmethod decorator, I think I have misunderstood how it works, the code is full of comments because otherwise it would give errors https://code.sololearn.com/cS4ek9Jnaxnd/?ref=app --

17th Feb 2022, 8:04 PM
Mick
Mick - avatar
16 Answers
+ 3
The constructor needs to be __init__, not init Use print() instead of pass to see what happens: https://code.sololearn.com/cWBLxKW7d8va/?ref=app
17th Feb 2022, 8:39 PM
Lisa
Lisa - avatar
+ 2
In Python3 you can sometimes omit the @staticmethod keyword. But it's bad practice because it is error prone: https://stackoverflow.com/questions/43587044/do-we-really-need-staticmethod-decorator-in-JUMP_LINK__&&__python__&&__JUMP_LINK-to-declare-static-method
17th Feb 2022, 10:47 PM
Simon Sauter
Simon Sauter - avatar
+ 2
By using obj = Class() you're creating an instance of Class. Since you have not defined the __init__() method for Class it defaults to the __init__() method of the object class. (See this for more information : https://www.sololearn.com/discuss/2941542/?ref=app). This object is passed to method0 when you call obj.method0() as self and from there as var to method1(). Remember that using "self" to refer to the instance is merely a convention you can just as well refer to it using "var" (although you really, really shouldn't).
18th Feb 2022, 11:01 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Ok, I understand that the decorator is not needed in python3 but it is in python2 so I will use it. Another problem, I don't understand who is initializing the self and var variable (of method 1)? Is it the Class () constructor? https://code.sololearn.com/cSGtXT6Inm5V/?ref=app So the constructor passes the address of its instance to all methods of the same class ?
18th Feb 2022, 10:41 PM
Mick
Mick - avatar
+ 1
self refers to the instance of the class, in your example obj is the instance. The constructor is __init__(self) – each class should have a constructor. method need to be either method1(self, var) or, to be a static method, the static method decorator. Then you declare it like method1(var) (without self) Anyway, you need to pass an argument to method1. That is the var. For instance obj.method1("msg"). It will give an error if you do not pass anything
18th Feb 2022, 10:52 PM
Lisa
Lisa - avatar
+ 1
Lisa That code works perfectly, as you can see the variable var has an address pointing to the instance, this like the self variable, even if I had used the __init __ () method nothing would have changed, I don't understand what loads those variables
18th Feb 2022, 10:57 PM
Mick
Mick - avatar
+ 1
It prints a memory address, a placeholder, not a variable value What is it supposed to do ? What do you mean "loads those variables"?
18th Feb 2022, 10:59 PM
Lisa
Lisa - avatar
+ 1
Simon Sauter yes thank you, I know it's not routine, it's just to make a mental order and understand this language
18th Feb 2022, 11:05 PM
Mick
Mick - avatar
+ 1
If you don't want to refer to the instance by self, try at least to always use the same name (not var and self innthe same class definition)
18th Feb 2022, 11:07 PM
Lisa
Lisa - avatar
+ 1
Depending on how you are going to use your class, the __init__() method may be omitted. https://stackoverflow.com/questions/6854080/is-it-necessary-to-include-init-as-the-first-function-every-time-in-a-class Class methods are used to create class objects ( similar to a constructor ) for different use cases. Static methods are used to create utility functions. If you're creating a class as a something like a functions collection, you can make something like this. https://code.sololearn.com/c5FNZYFAR2Qe/?ref=app but if your class is going to be instantiated, better use @staticmethod. Also, undecorated static methods raises an error if called in certain ways. See the comments inside my code. https://python-reference.readthedocs.io/en/latest/docs/functions/staticmethod.html
19th Feb 2022, 10:12 AM
Bob_Li
Bob_Li - avatar
+ 1
Why use static method: "Static methods have a limited use case because, like class methods or any other methods within a class, they cannot access the properties of the class itself. However, when you need a utility function that doesn't access any properties of a class but makes sense that it belongs to the class, we use static functions."
19th Feb 2022, 12:00 PM
Bob_Li
Bob_Li - avatar
0
Sorry Lisa, typo, thanks for telling me
17th Feb 2022, 11:33 PM
Mick
Mick - avatar
0
Simon Sauter thanks for that, I'll give it a careful read, as I've removed all decorators from my classes (I think I'm wrong now)
17th Feb 2022, 11:35 PM
Mick
Mick - avatar
0
Lisa I mean initialization
18th Feb 2022, 11:03 PM
Mick
Mick - avatar
0
ok init is not a constructor, alas the other more complicated and strongly typed languages ​​do not help and not even the simplicity of python helps
18th Feb 2022, 11:08 PM
Mick
Mick - avatar
0
I understand Lisa, it's just an example, thanks anyway
18th Feb 2022, 11:09 PM
Mick
Mick - avatar