473,587 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2332
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*******@gmai l.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...@gmai l.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7702
by: Salmo Bytes | last post by:
I want to send an xml string as a get parameter, where the xml is created from a runtime database query. I try to avoid dynamic framesets, but sometimes they're needed: $xml = mysql_stuff($x,$y,$z); echo "<frameset...>"; echo "<frame src=".$PHP_SELF."?xml=".$xml.">"; snip....
110
9868
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
2
2433
by: Yarik | last post by:
Hello there! I am working with MS SQL Server 2000. I have a table function that takes an integer parameter and returns a table, and I can successfully use it like this (passing a literal as a parameter): SELECT * FROM MyTableFunction(1)
0
3313
by: Zlatko Matić | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems appear when someone is trying to use it as front-end for real server database systems such as PostgreSQL or MySQL. One of these problems is regarding...
5
3409
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? /******************************************************** Below is my code: ********************************************************/
4
149327
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: session_set_save_handler(array(&$sess_handler, 'open'), array(&$sess_handler, 'close'), array(&$sess_handler, 'read'), array(&$sess_handler, 'write'),...
5
7809
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into a WebService is that essentially we are passing an address of an object which resides on the 'local machine' i.e. a local machine object address....
14
20383
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case...
12
11048
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed!...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.