question od default args

Sean 'Shaleh' Perry shalehperry at attbi.com
Wed Jun 26 13:48:56 EDT 2002


On 26-Jun-2002 Gonçalo Rodrigues wrote:
> Hi,
> 
> When I want default args I usually do
> 
> def (arg = None):
>     ...
> 
> But what if I want to make None a reasonable argument too? That is, I
> want to know if the user has in fact passed an argument, and if not to
> do something special, with None (or whatever object) being a reasonable
> arg to be passed.
> 

>>> def func(arg1, *args):
...   if len(args):
...     print "user gaves us %d args" % (len(args) + 1,)
...   else:
...     print "only have arg1"
... 
>>> func('sean')
only have arg1
>>> func('sean', 'perry')
user gaves us 2 args

this means you need to throw an exception if you only want 3 args and the user
gives you 4 (or more).





More information about the Python-list mailing list