Suppose I have an instance method that contains a lot of nested conditionals. What would be a good way to encapsulate that code? Put in another instance method of the same class or a function? Could you say why a certain approach is preferred?
Suppose I have an instance method that contains a lot of nested conditionals. What
Share
If the function is only used by one class, and especially if the module has more classes with potentially more utility functions (used only by one class), it might clarify things a bit if you kept the functions as static methods instead to make it obvious which class they belong to. Also, automated refactorings (using the e.g. the rope library, or PyCharm or PyDev etc) then automatically move the static method along with the class to wherever the class is moved.
P.S.
@staticmethods, unlike module-level functions, can be overridden in subclasses, e.g. in case of a mathematical formula that doesn’t depend on the object but does depend on the type of the object.