What value should be passed to make a function use the defaultargument value?

Fredrik Lundh fredrik at pythonware.com
Wed Oct 4 07:07:56 EDT 2006


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)

</F> 






More information about the Python-list mailing list