So I have recently discovered why static methods in python are so useful.
While writing some code, and going over some existing ones, I couldn't help but change many (or all actually) of the functions to static, given the ease to call the functions from outside of the class, without having to really 'initialise' the class.
Of course, this means changing the the structure of the code such that I don't reference 'self' in the static function, but instead just pass in the parameter as an argument of the function.
For example, previously I had something like:
def pot_size(self, pre_flop=False):
# Need something different _1_pre_flop because there is nothing in the screenshot _1_pre_flop
if pre_flop:
sum_of_action_behind_me = 0
for pos in self.action_so_far_f:
try:
sum_of_action_behind_me += float(self.action_so_far_f[pos])
except:
pass
return sum_of_action_behind_me
So in the above I change the function to be static, and pass 'action_so_far_f' directly into the function when calling it, so I don't need the 'self'.
I am really wondering why we would ever use non-static methods... I am sure I'm missing something big here but looking on the internet I couldn't find what it was.
The short answer is: you don't have to.
In fact, there are many programming languages that do not have a class structure. Take, for example, C, which is purely imperative, or purely functional languages, like Haskell. Having methods called on a class is a part of the Object-oriented programming paradigm, which python supports, but doesn't (really) enforce.
If you like this style of programming, you might really like python's dataclasses, which allow you to create structured data without even having to write an
__init__.What most people would say, tough, is that creating non-static methods in python can still be useful for a few things. For example, you might find use in
@classmethodfor different constructors. Maybe you want to create some structured data from json, where callingDataClass.from_json()is cleaner than a seperatedata_class_from_json()method, among other examples. The reason for this, in short, is structuring your code. By putting methods that only make sense in the context of a class inside that class, you immediately organise the stuff you write.If you start following Object-oriented code design patterns, there are going to be a ton more reasons, from strategy patterns to polymorphism to interfaces. But, you don't have to do any of that. Python isn't Java.
The only part in python where it is absolutely necessary to create non-static methods is for dunder methods. For example,
__le__, where creating a method like this is the only way to get the behaviour of operator overloading.Basically, it's a style to do it, and it's a style not to do it. People have tons of arguments over which is better, but if you want to be reasonably senoir, you should probably be familiar with both. Learn yourself a little Haskell, learn yourself a little gang-of-four, and make sure you understand the lingo no matter what paradigm people are working in.
Overall, tough, the honest main reason why I still use non-static methods a lot: to not break excising style. You shouldn't start programming in a FP style in an OOP project and vice-versa.