0
What exactly are static methods and how are they different from normal methods in Python.
I am studying Oops in python and as far as I understand in oops, the word static means belonging to the class. If functions are already public in python, doesn't that make every function static by default? Except the fact that the static method doesn't need a self as parameter, what is the use of static method. Please elaborate.
6 Antworten
+ 4
You may be interested in this article:
https://pythonbasics.org/static-method/
Demonstration:
https://code.sololearn.com/cwLl9Bk30gre/?ref=app
+ 4
Static methods don't use a 'self' parameter. Which means static methods can not access other methods or attributes inside the class. Useful when a simple function you make doesn't depend on any other methods, just internally
+ 1
Static methods are more of a way to break down larger methods within a class. Say you need a function to take a list and then extract specific information from it. Technically, if the list is provided as an argument, then in theory, that's all you really need to execute the function, no need for a self parameter because you don't use it within the function. ALSO, it helps you and other programmers see that the function does not manipulate any class attributes.
Look at my module. It contains many static methods. They are like that because no class attributes were needed. And also my IDE doesn't like it when i create a regular method with no class attributes or methods. You can do it no problem it'll still work. But if it's not needed, why add it? Just more clutter if you ask me
0
Oh okay, so a static method can run with and without objects while a normal method needs objs to call the fns.Thanks Lisa
0
If the static methods are class based, why can't they access the attributes and methods present in the class. Please elaborate a bit Slick. Thanks
0
So, any function that doesn't change the attributes of a class and doesn't need the other non-static functions of the class, it can be made static. Thanks Slick