default argument depending on another argumen?

Gerhard Häring gerhard.haering at gmx.de
Thu Nov 7 17:54:02 EST 2002


Marco Herrn wrote in comp.lang.python:
> Hi, 
> 
> I want to define a function like this:
> 
> def foo(self, bar, x= bar.x):
>     ....
>     pass
> 
> Now I have the problem that 'bar' isn't really known when defining this
> function.
> So how can I achieve this behaviour?

There's a FAQ entry o this, too IIRC :-)

The workaround looks like this:

def foo(self, bar, x=None):
    if x is None:
        x = bar.x
    ...

-- Gerhard



More information about the Python-list mailing list