开发者

"Method overloading" in python

I think the concept is called overloading. Right now I'm writing a class that will provide getter and setter methods, and am stuck on a design issue.

Both methods are one-liners that simply set a value, or return a value.

def set_number(self, num):

   self.count = num

def get_number(self):

   return self.count

Would it be better to save space and make the class "look" smaller by doing something that will basically combine the two methods into one and then just decide which line should be executed depending on whether the num argument is provided?

Or should I just stick to clarity and keep them separated? Some people feel that it's a "waste of space" to keep all these one-liners on their own, while others disagree and开发者_Go百科 prefer splitting them up.

Any reasons why I would choose one or the other?


In Python, you should prefer not to use getters and setters at all. Instead simply reference the count attribute directly, e.g. print instance.count or instance.count = 5

The point of getters and setters in other languages is for encapsulation and for future-proofing in case you need to add logic to the getters or setters. In Python you can accomplish this by later using property, which will not break your existing API.

@property
def number(self):
     # do extra logic if necessary
    return self.count

Properties can also have setters - see: http://docs.python.org/library/functions.html#property

Python is not Java. =)

Extra bonus reading material:

  • http://tomayko.com/writings/getters-setters-fuxors
  • http://eli.thegreenplace.net/2009/02/06/getters-and-setters-in-python/


Generally in python it's very bad style to use explicit getter/setter methods. Just do something like this:

In [1]: class Foo(object):
   ...:     def __init__(self):
   ...:         self.num = 1
   ...:         
   ...:         
In [2]: f = Foo()
In [3]: f.num
Out[3]: 1
In [4]: f.num = 2
In [5]: f.num
Out[5]: 2

If you really need logic, you can preserve this same API at a later date by using properties. Notice how the same interface is preserved below while still adding functionality.

In [8]: class Foo(object):
   ...:     def __init__(self):
   ...:         self._num = 1
   ...:     @property
   ...:     def num(self):
   ...:         return self._num
   ...:     @num.setter
   ...:     def num(self, num):
   ...:         if num < 0:
   ...:             raise ValueError("Your Foo would be too small!")
   ...:         self._num = num
   ...:         
   ...:         
In [10]: f = Foo()
In [11]: f.num
Out[11]: 1
In [12]: f.num = 2
In [13]: f.num
Out[13]: 2
In [14]: f.num = -1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/Daenyth/<ipython console> in <module>()

/home/Daenyth/<ipython console> in num(self, num)

ValueError: Your Foo would be too small!

Regarding "overloading" -- That term does not apply here. That is the term for changing the behavior of operators like +, ==, and so on, by changing the definition for those methods on your object. You can do some overloading in python via __cmp__, __add__, and so on.


I would leave it as two separate methods for the sake of code readabilty and maintainabilty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜