I come from a Java background and I\'m new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing file
One other aspect of Python static methods is that when called on instances, they are polymorphic on the type of the variable they are called on, despite not having an instance of self
.
For example:
class BaseClass:
@staticmethod
def my_method():
return 0
class SubclassLeft:
@staticmethod
def my_method():
return "left"
class SubclassRight:
@staticmethod
def my_method():
return "right"
instances = [BaseClass(), SubclassLeft(), SubclassRight()]
for i in instances:
print(i.my_method())
Running this results in
$ python example.py
0
left
right