0

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:

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:

class Cat(object):
    def __init__(self, name):
        self.name = name

    def meow(self):
        meow(self.name)

    @staticmethod
    def meow(name): # <-- Granted, Python doesn't overload method names
        print "{} meowed cutely!".format(name)

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:

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)

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.

pk-nb
  • 2,850
  • 1
  • 18
  • 20
  • 1
    The real question is, what do you need to do that takes both the form of `a_instance.b()` and `A.b(a_instance)`? Why is one or the other not sufficient? – Amber Jul 01 '12 at 01:30
  • I'll admit this example is really contrived, but during a series of [Objective-C lectures](http://itunes.apple.com/us/itunes-u/ipad-iphone-application-development/id473757255) the instructor had us convert the existing implementation of a calculator and make some convenience static / class methods to call it without an instance. I guess I'm not sure how often that comes up, and how I should prepare for future extensions. – pk-nb Jul 01 '12 at 01:41
  • Note that Objective C is not Python, and some things that make sense in Objective C do not make sense in Python. It is actually very rare to have static methods in most Python code. Most of the time in Python when you think you might use a static method, you just write a non-method function instead. – Amber Jul 01 '12 at 01:55

1 Answers1

1

You can add optional parameters and keyword parameters to a function without breaking existing uses of it. For example, you could add a protocol=old argument to the end to choose which behavior to use, with the old behavior being default in the case of no explicit parameter. This is the best option if you want to keep the same function name, but it can quickly get unwieldy if you do it multiple times.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • Could you give an example of that? Or something lead to search on? I am unexperienced with protocol=old argument / multiple parameters and not quite sure what to google for that. – pk-nb Jul 01 '12 at 02:04
  • Say that the old api has a function called foo which takes a number to print out. You want to create a new version which prints out twice the number. You can do something like this. `def foo(num, newVersion=False): if newVersion: print num*2 else print num` – Antimony Jul 01 '12 at 03:57
  • So the parameter can be overridden if you pass a different value. That's pretty clean and simple. Thanks. – pk-nb Jul 01 '12 at 13:00