Pass same parameter in Recursive function

Chris Rebert clp at
Wed Sep 3 05:57:52 CEST 2008

Assuming the function is tail-recursive or the "unchanging" arguments
are immutable, just use a closure:

def func(self, x, y, A, B, C):
    def _func(x,y):
        return _func(g(A,B,C,x), h(A,B,C,y)) #recurse
    return _func(x, y)

I'm unsure as to the performance impact of this though.

- Chris

On Tue, Sep 2, 2008 at 8:20 PM, Davy <zhushenli at> wrote:
> Hi all,
> Sometimes I need to pass same parameter in recursive function. From my
> point of view, the style is redundant, and I don't what to use some
> global style like self.A, self.B, Is there any other choice?
> For example,
> def func(self, x, y, A, B, C):
>  #x, y change in recursive call
>  #A, B, C change in the first layer function call, but did not change
> in recursive call
>  if (...):
>    func(x, y, A, B, C)
>  else(...):
>    func(x, y, A, B, C)
> Best regards,
> Davy
> --

Follow the path of the Iguana...

More information about the Python-list mailing list