What value should be passed to make a function use the defaultargument value?
Antoon Pardon
apardon at forel.vub.ac.be
Wed Oct 4 07:26:00 EDT 2006
On 2006-10-04, Fredrik Lundh <fredrik at pythonware.com> wrote:
> Georg Brandl wrote:
>
>>> But that can only work if you are the author of f. Take the
>>> following code:
>>>
>>> def myrepeat(obj, times = xxx):
>>> return itertools.repeat(obj, times)
>>>
>>> What value do I have to substitue for xxx, so that myrepeat
>>> will have the exact same function as itertools.repeat?
>>
>> There's no possible value. You'll have to write this like
>>
>> def myrepeat(obj, times=None):
>> if times is None:
>> return itertools.repeat(obj)
>> else:
>> return itertools.repeat(obj, times)
>
> or:
>
> def myrepeat(*args):
> return itertools.repeat(*args)
Yes that works but I have the impression that this solution
becomes complicated very fast once you want to do extra
processing in the function body. Take the following
def myrepeat(obj, times = xxx)"
newobj = Process(obj)
return itertools.repeat(obj, times)
I think it would become something like:
def myrepeat(*args):
obj = args[0]
tail = args[1:]
newobj = Process(obj)
newargs = (newobj,) + tail
return itertools.repeat(*newargs)
More information about the Python-list
mailing list