+ 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 --
16 Respuestas
+ 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
+ 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
+ 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).
+ 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 ?
+ 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
+ 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
+ 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"?
+ 1
Simon Sauter yes thank you, I know it's not routine, it's just to make a mental order and understand this language
+ 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)
+ 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
+ 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."
0
Sorry Lisa, typo, thanks for telling me
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)
0
Lisa I mean initialization
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
0
I understand Lisa, it's just an example, thanks anyway