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.

18th Jul 2022, 4:32 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
6 ответов
+ 4
18th Jul 2022, 4:50 PM
Lisa
Lisa - avatar
+ 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
18th Jul 2022, 5:03 PM
Slick
Slick - avatar
+ 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
20th Jul 2022, 8:54 PM
Slick
Slick - avatar
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
20th Jul 2022, 8:19 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
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
20th Jul 2022, 8:20 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
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
21st Jul 2022, 2:10 AM
Ruchika Sehgal
Ruchika Sehgal - avatar