question od default args

Cliff Wells logiplexsoftware at earthlink.net
Wed Jun 26 13:49:23 EDT 2002


On Wed, 26 Jun 2002 18:45:25 +0100
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.

How about:

def foo(*args, **kwargs):
    argc = len(args) + len(kwargs)
    if argc == 0:
        print "no arguments"
        # set default values
    else:
        print argc, "arguments"
        # assign arguments to attributes or whatever

Then if the user passes None as an argument, argc will still be 1.  You can
then get the arguments from args and kwargs.

Regards,
-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308





More information about the Python-list mailing list