optional arguments

Alex Martelli aleax at aleax.it
Mon Sep 17 13:06:04 CEST 2001


"Silvio Arcangeli" <sarcangeli at montrouge.sema.slb.com> wrote in message
news:mailman.1000716020.11396.python-list at python.org...
> Hello everybody,
> sorry for this question that may seem silly.
> I have an object whose __init__  looks like the following:
>
> class Connection:
> def __init__(self, ip=def_ip, port=def_port)
> ...
>
> I have to call two different functions when the object is instantied like
> c=Connection()
> and when it is instantiated like
> c=Connection(def_ip, def_port)
>
> how can I tell wheter no arguments were passed from the user or whether
> they were passed but they were just like the default values?

By *NOT* using, as default values for the arguments, values
that could indeed equally well be passed by the client-code.

class Connection:
    class __Boo: pass
    __None = __Boo()
    def __init__(self, ip=__None, port=__None):
        if ip is self.__None:
            print "ip was NOT passed"
        else: print "ip was passed as",ip

etc, etc.

See http://www.penguin.it/pipermail/python/ if you'd
like help and/or discussion about Python in Italian.


Alex






More information about the Python-list mailing list