com_addbyte

Thomas Wouters thomas at xs4all.net
Thu Nov 2 03:41:47 EST 2000


On Wed, Nov 01, 2000 at 11:52:26PM +0000, etsang at my-deja.com wrote:

> Hi, but is it possible to somehow set default of a particular argument
> list. Since it is  long arg list and there a couple functions using
> with each need to modify the list for its needs. So how can I:

> 1. set a default argument list and have each calling func to decide
> which parameter it wants to use default values or not?

Simple: don't use default arguments. Or rather, use a recognizable default
argument for each default, like 'None', or perhaps a custom class (if None
is a valid value for normal values), and then handle them in the function,
going over each argument, checking whether it should use the passed-in
value, the 'default' value (different than the one in the agument list,
possibly), or some other value.

def sister(x=None, y=None, z=None):  # because it doesn't know what it wants
    if x is not None:
        # Should 'x' be the value passed in, or a 'default' value
        x = default_x_or_x(x)
    # Or, depending on your values, this might work
    y = y or default_y

    # Or something like this
    z = usable(z) or default_z

> 2. calling function just supply the parameters that need tobe changed
> Can this be done in agr list?

No, but it can be done with keyword arguments:

>>> def spam(x=1, y=2, z=3):
...     return x,y,z
... 
>>> spam(z=5)
(1, 2, 5)
>>> spam(x=2)
(2, 2, 3)
>>> spam(5, z=100)
(5, 2, 100)

You really to read up on arglists, keyword arguments, and apply() :-) The
tutorial and the language reference are more in-depth than this example.
(http://www.python.org/doc/)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list