Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding convenience static / class methods without removing or breaking existing implementation
    text
    copied!<p>I am getting the hang of the OOP paradigm, and the art of making expandable and reusable code is something I want to improve at. Let's say in theory that I have a Python library of utility classes that has been widely used. I want to add some convenience static methods with the same code to a particular class for ease of use, but I don't want to break my existing use of the library. What is the recommended way to implement and name the new class methods? I have a couple of guesses as follows:</p> <p>1) With an overloaded method name to maintain clarity? Python's not really a good example, but in other languages such as Java? For example:</p> <pre><code>class Cat(object): def __init__(self, name): self.name = name def meow(self): meow(self.name) @staticmethod def meow(name): # &lt;-- Granted, Python doesn't overload method names print "{} meowed cutely!".format(name) </code></pre> <p>2) With a different, perhaps less semantic static method name? The old name cannot be changed, so... This seems to me this could get out of hand for huge projects, with a bunch of non semantic names for just a static version of the same method. Example:</p> <pre><code>class Cat(object): def __init__(self, name): self.name = name def meow(self): meowFrom(self.name) @staticmethod def meowFrom(name): # Different, possibly less semantic name print "{} meowed cutely!".format(name) </code></pre> <p>I assume duplicating the code outright is a bad idea. Which is the way to go? Or is there some better design pattern? Something specific for Python I am unaware of? I want to make sure my code isn't worthless in the future; and make some personal libraries that are expansive for future projects.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload