There are two static methods in my class:
class my_class():
def main(self, input):
pass
@staticmethod
def static_1():
#repeated_code
pass
@staticmethod
def static_2():
#repeated_code
pass
As they share some #repeated_code
, I want to reduce the repetition in them by writing a function def repeat()
for the #repeated_code
.
Since they are inside static methods, I couldn't call a class method via self.repeat()
.
It doesn't seems reasonable to write the function outside the class, because the function repeat
is only used inside the class.
How should I achieve the propose of avoiding repeating myself?
I'm not sure this is the right way to go - static methods are meant to be small convenience functions, and if you have to share code, they aren't small.
But, if you want to, you can call a static method by referencing the class by name, like this:
class test():
@staticmethod
def repeat():
# shared code
pass
@staticmethod
def static_1():
test.repeat()
@staticmethod
def static_2():
test.repeat()