471,897 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,897 software developers and data experts.

Pass same parameter in Recursive function

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
Sep 3 '08 #1
3 2167
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 <zh*******@gmail.comwrote:
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
--
http://mail.python.org/mailman/listinfo/python-list
--
Follow the path of the Iguana...
http://rebertia.com
Sep 3 '08 #2
On Sep 3, 11:57*am, "Chris Rebert" <c...@rebertia.comwrote:
Assuming the function is tail-recursive or the "unchanging" arguments
are immutable, just use a closure:
[SNIP]
Hi Chris,

Thank you :)
Perhaps I should clarify the problem.
1. the function is NOT tail-recursive
2. Yes, the arguments are immutable after I call the function the
first time.

I think the best description of the problem may be:
How to keep some argument constant and accessable in one function
(especially, recursive function). We know that we can use something
like self.parameter to keep the parameter constant and accessable in
one class.
>
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 <zhushe...@gmail.comwrote:
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
--
http://mail.python.org/mailman/listinfo/python-list

--
Follow the path of the Iguana...http://rebertia.com- Hide quoted text -

- Show quoted text -
Sep 3 '08 #3
Davy schrieb:
On Sep 3, 11:57 am, "Chris Rebert" <c...@rebertia.comwrote:
>Assuming the function is tail-recursive or the "unchanging" arguments
are immutable, just use a closure:
[SNIP]
Hi Chris,

Thank you :)
Perhaps I should clarify the problem.
1. the function is NOT tail-recursive
2. Yes, the arguments are immutable after I call the function the
first time.

I think the best description of the problem may be:
How to keep some argument constant and accessable in one function
(especially, recursive function). We know that we can use something
like self.parameter to keep the parameter constant and accessable in
one class.

The same way chris showed you - using a closure, you can capture
variables outside, and only pass the changing params.

The tail-recursion property isn't needed for that.

Diez
Sep 3 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Salmo Bytes | last post: by
5 posts views Thread by wilson | last post: by
5 posts views Thread by David++ | last post: by
14 posts views Thread by Abhi | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.