handling different arguments depending on first argument

logistix logstx at bellatlantic.net
Sat Apr 27 16:17:02 EDT 2002


>>> def fun(required, *optional, **keywords):
...  print "Required => %s" % required
...  print "Optional => %s" % repr(optional)
...  print "Keywords => %s" % repr(keywords)
...
>>> fun(1,2,3,4,5)
Required => 1
Optional => (2, 3, 4, 5)
Keywords => {}

>>> fun(1,2,3)
Required => 1
Optional => (2, 3)
Keywords => {}

>>> fun(1,2,3,4, keyword1=4, keyword2=5)
Required => 1
Optional => (2, 3, 4)
Keywords => {'keyword2': 5, 'keyword1': 4}

--
-

"Paul Miller" <paul at fxtech.com> wrote in message
news:3CCAF77B.3BEBF259 at fxtech.com...
> I've written a C function (callable from Python) that can take a
> variable number of arguments, and the arguments after the first are
> dependent on what the first was. For example, the signatures for 2
> "modes" might look like this:
>
> my_func(MODE1, var1, var2, var3=None)
> my_func(MODE2, var1, var2=None)
>
> etc.
>
> Is it possible to handle this kind of different calling convention in
> pure Python? I'd like to write the equivalent of an "overloaded" Python
> object constructor, which takes various arguments depending on the
> first.





More information about the Python-list mailing list