I’m a big fan of Python. It has little quirks that can be annoying (like having to specify self.method() instead of using scoping rules to look at the class first), but overall I really like the language. But today I ran into a shortfall that I really stubbed my toe on: Python (as of 2.5) doesn’t support function overloading.
For example, this snippet of code won’t run:
def a():
print “This is a”def a(x):
print “This is a(x)”a()
Instead it will just tell you that TypeError: a() takes exactly 1 argument (0 given) presumably because the second function (a(x)) seems to overwrite the reference to the first one. Further reading on the topic shows that a fix for this might be coming in the future (see Guido’s blog entry for details).
The strange thing for me is I never noticed this until recently. The only reason I ran into it was because I wanted to do some refactoring where it made sense to have an overloaded method. Or at least that’s what I would have done in Java.
Not having this option available is having an interesting side effect in that it made me step back a little bit and evaluate the “big picture” a little bit more than I normally do. Which in this case isn’t a bad thing, hopefully it will cause some me to stretch in new directions and discover my inner OO-Architect…
3 comments ↓
Try http://docs.python.org/tut/node6.html#SECTION006730000000000000000 or http://www.python.org/doc/current/tut/node6.html#SECTION006710000000000000000 or http://www.python.org/doc/current/tut/node6.html#SECTION006720000000000000000 .
def a(x = None):
if x is None:
print “Found None”
else:
print “x = “, x
# then try
a()
# And contrast to
a(1)
- Paddy
Hi Paddy,
While I agree with your code example, that’s actually one of the things I was trying to move away from. I had a ton of code that had all kinds of conditional statements, and I felt that if certain stars aligned I could get rid of them and streamline things.
Giving default argument values for the object is a good way to get this behavior, I just kinda felt like it wasn’t the “OO” way.
But the more and more I think about this (I just deleted a whole paragraph defending my old position), the more I’m starting to think that this might be a better way. Perhaps I have my head to far in the Java world and need to think more pythonically.
Thanks for the posting, I’m gonna go give this a try and see what shakes out.
-Nick
Your welcome
You must log in to post a comment.