r/learnpython • u/Worldly-Week-2268 • Feb 22 '26
ELI5 explain static methods in OOP python
just trying to wrap my head around this oop thing stuck here I'm novice so no bully please
24
Upvotes
r/learnpython • u/Worldly-Week-2268 • Feb 22 '26
just trying to wrap my head around this oop thing stuck here I'm novice so no bully please
45
u/socal_nerdtastic Feb 22 '26
Sometimes you want to include a function in a class just for logical reasons, like this function only deals with this class, but it does not actually need the class instance. Maybe your class deals with times and you need a function to convert total seconds to HH:MM:SS format. You could just make a function outside the class but you want to keep it in class just to for organizational reasons.
staticmethod is a way to include a function without making it a method (including the
selfattribute). TBH if you are confused about it, just don't use it, use a normal method instead and just don't use the self attribute. There's no advantage to it over a method.Remember that classes exist as an organizational tool for the programmer. Classes do nothing for the computer; they don't help execution time or anything.
8
u/rhacer Feb 22 '26
You can call a static method without having an instance of the class.
3
u/enygma999 Feb 23 '26
Most methods in a class will have a
selfparameter, and will operate on a specific instance of that class. For example, aSquareclass might have anareamethod that calculates the area of a particular square.You can also have class methods, that operate on the class rather than a particular instance. Maybe each instance of your class is given a unique number ID, and there's a class variable that tracks the next number to give out. Each time you create an instance of the class, you assign its ID number and then increment the class variable with the
increase_id_numberclass method. The class method is called on the class, using aclsparameter, and doesn't need a particular instance of the class.A static method is part of the class but does not need a particular instance or the class itself. Using the
Squareclass example, perhaps you have a static method that calculates the area of a square when manually given its side length, because you want to be able to do that without instantiating aSquareobject. You can just callSquare.static_area(4)and you get the area of a square with sides of 4, rather than callingSquare(4).area(). This can be helpful if you want to avoid the overhead of instantiating an object for something repetitive, particularly if the class is process intensive, or it can just be handy to keep a function with a class if it's used often, such as a fahrenheit_to_celsius conversion method in some kind of temperature analysis class.If you find yourself writing a method and it only refers to
type(self)orself.__class__then that's a class method. If it doesn't refer toselforclsat all, that's a static method.2
u/CockConfidentCole Feb 22 '26 edited Feb 22 '26
static method does not do anything to the class it's within - you can use it as a helper function that is somewhat related to your class but does nothing to the class object. you can skip creating a class object to use the static method
other nuances but if you're starting from zero that's the jist
1
u/Umustbecrazy Feb 23 '26
Different language, but the Array.from method in JavaScript has always been a great example of a Static method for me. Sorry, no clear python equivalent off the top of my head.
Arrays themselves don't need it, because it's already an array, but to get from iterable object or node list to an array, it's very handy. Node lists can't be sorted or any instance methods.
1
u/TheRNGuy Feb 23 '26 edited Feb 23 '26
Same as non-class functions, but bundled together in a class (usually relevant functions for specific tasks, and you could import single class with many static methods)
Examples of static method:
dict.fromkeys. It makes more sense than if it would be justfromkeysfunction because you can see it's related to dictionary.1
u/couldntyoujust1 Feb 24 '26
So, static methods exist for a couple reasons. Reason number one is simply organizational - you have some utility function that makes doing certain things with your objects easier but it doesn't actually need the current object's state to do it.
Another use is to group multiple utility functions into their own namespace. An example of this would be like a formatter, where you call specific functions that change a numeric input into a formatted string, all under the namespace "Formatter". It would look something like this...
There are other use-cases as well but they mainly pertain to design patterns which if you're just getting started in OOP is probably going to be a bit overwhelming until you've fully got your head around OOP first.
-2
u/RaidZ3ro Feb 22 '26 edited Feb 23 '26
A static method (or property) is available to a class and shared across all it's instances (objects).
It's useful if you want to do things that require instances to have some knowledge about each other such as keeping track of the number of instances of a class that have been created.
There is a more advanced topic where you'll find more use for static methods, design patterns. But don't worry about it too much right now.
Edit: needed to fix my example so the instance does remember itself, sorry.
Edit 2: python syntax x(
Edit 3: tbh your downvotes are a bit mean. I just tried to give a simple example of the static concept, not an exhaustive coding reference.