function argument dependent on another function argument?

Aaron Brady castironpi at gmail.com
Sun Jan 18 21:45:11 EST 2009


On Jan 18, 12:42 pm, andrew cooke <and... at acooke.org> wrote:
> >     sentinel = object()
> >     ...
>
> >     def foo(x, y=sentinel):
> >       if y is sentinel:
> >           y = self.a
>
> it just struck me you could also do:
>
>      def foo(self, x, *y_args)
>        y = y_args[0] if y_args self.a
>
> which more directly checks whether an argument was passed, but has the
> downside of making the method signature less clear in the declaration.
>
> andrew

Also, if you need to change your calling signature down the line, this
alternative really ties your hands with regard to it.  You also lose
the ability to pass 'y' by keyword.

George Sakkis, who I only know from the NG, has a recipe that tests a
call against a function signature to determine what arguments are
being passed.  The 'inspect' module also has the 'getargvalues'
function, which does something similar.



More information about the Python-list mailing list