On 2/23/2010 1:25 PM, Michael Rudolf wrote:
Just a quick question about what would be the most pythonic approach in
this.
In Java, Method Overloading is my best friend, but this won't work in
Python:
>>> def a():
pass
>>> def a(x):
pass
>>> a()
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
a()
TypeError: a() takes exactly 1 argument (0 given)
So - What would be the most pythonic way to emulate this?
Is there any better Idom than:
>>> def a(x=None):
if x is None:
pass
else:
pass
Consider this:
#------------------
def myfunc(*arglist):
if not arglist:
print "no arguments"
return
for i, arg in enumerate(arglist):
print "Argument %d is a %s, with value:" % (i, type(arg)),
print arg
myfunc()
print "---"
myfunc(1)
print "---"
myfunc(2, "three", [4, 5,6])
#------------------
program output:
no arguments
---
Argument 0 is a <type 'int'>, with value: 1
---
Argument 0 is a <type 'int'>, with value: 2
Argument 1 is a <type 'str'>, with value: three
Argument 2 is a <type 'list'>, with value: [4, 5, 6]
HTH,
John
--
http://mail.python.org/mailman/listinfo/python-list