Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Gregory Ewing
Yawar Amin wrote: Cool ... but it looks like this can still potentially hit the max recursion limit? It depends on the nature of your data. If the data is a tree, it's very unlikely you'll reach the recursion limit unless the tree is massively unbalanced. If there's a chance of that, or if th

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Gregory Ewing
Roy Smith wrote: I will commonly put something like: import logging logger = logging.getLogger("logger-name-for-my-module") But that's not really a global variable, it's a global constant. There's nothing wrong with those, we use them all the time -- classes, functions, etc. If you were to re

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Chris Angelico
On Sun, Jan 18, 2015 at 4:30 AM, Albert van der Horst wrote: > The proper technique is make the global local to the normal subroutine, > then make the subroutine with those parameters you don't want to see > also local to that subroutine. > E.g. > > def fib(n): > ' return the n-th Fibonacci nu

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Albert van der Horst
In article <5e4ccec6-7a00-467d-8cf6-258ab0421...@googlegroups.com>, Tim wrote: >I have this type of situation and wonder if I should use a global >variable outside the recursive function instead of passing the updated >parameter through. > >I want to get a union of all the values that any 'things

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Roy Smith
In article <54ba3654$0$13008$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > Good reasons for using global variables are few and far between. Just about > the only good reason for using global variables that I can think of is if > you have one or more settings/preference that get s

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Steven D'Aprano
Tim wrote: > I have this type of situation and wonder if I should use a global variable > outside the recursive function instead of passing the updated parameter > through. To a first approximation, the answer to: "I have a X, should I use a global variable or a parameter?" is *always* "use a p

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Yawar Amin
On Friday, January 16, 2015 at 9:24:15 PM UTC-5, Yawar Amin wrote: > [...] > vals.extend(curr_obj.values()) Ah, I should mention that the above will do a breadth-first search. If we want to do a depth-first search we simply replace the above line with: vals.extendleft(curr_obj.values(

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Yawar Amin
On Friday, January 16, 2015 at 1:34:51 PM UTC-5, Peter Otten wrote: > [...] > I recommend that you use a generator: > > >>> def walk(obj): > ... if not hasattr(obj, "keys"): > ... return > ... if "things" in obj: > ... yield obj["things"] > ... for v in obj.values(): >

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Chris Angelico
On Sat, Jan 17, 2015 at 9:20 AM, Gregory Ewing wrote: > The only thing I would change is to wrap it all up > in a top-level function that takes care of creating > the result set and returning it. > > def walk(obj): > res = set() > _walk(obj, res) > return res > > def _walk(obj, res): > ...

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Gregory Ewing
Tim wrote: I have this type of situation and wonder if I should use a global variable outside the recursive function instead of passing the updated parameter through. No! Globals are evil, at least for that sort of thing. The way you're doing it is fine. The only thing I would change is to wra

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Tim
On Friday, January 16, 2015 at 1:34:51 PM UTC-5, Peter Otten wrote: >> Tim wrote: >> > Globals are generally bad as they make code non-reentrant; when two calls of > the function run simultaneously the data will be messed up. > > I recommend that you use a generator: > > >>> def walk(obj): > ..

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Peter Otten
Tim wrote: > I have this type of situation and wonder if I should use a global variable > outside the recursive function instead of passing the updated parameter > through. > > I want to get a union of all the values that any 'things' key may have, > even in a nested dictionary (and I do not know

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Rustom Mody
On Friday, January 16, 2015 at 11:26:46 PM UTC+5:30, Chris Angelico wrote: > On Sat, Jan 17, 2015 at 4:49 AM, Tim wrote: > > I want to get a union of all the values that any 'things' key may have, > > even in a nested dictionary (and I do not know beforehand how deep the > > nesting might go): >

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Chris Angelico
On Sat, Jan 17, 2015 at 4:49 AM, Tim wrote: > I want to get a union of all the values that any 'things' key may have, even > in a nested dictionary (and I do not know beforehand how deep the nesting > might go): > > d = {'things':1, 'two':{'things':2}} > > def walk(obj, res): > if not hasatt

Re: Recursive function

2013-03-05 Thread Neil Cerutti
On 2013-03-05, Ana Dion?sio wrote: > Yes, I simplified it a lot. > > I need to run it 24 times. What I don't really understand is > how to put the final temperature (result) in it = 0 in temp_-1 > in it =1 One way to compose a function that recurses by calling itself is to first write the simples

Re: Recursive function

2013-03-05 Thread Ana Dionísio
Yes, I simplified it a lot. I need to run it 24 times. What I don't really understand is how to put the final temperature (result) in it = 0 in temp_-1 in it =1 -- http://mail.python.org/mailman/listinfo/python-list

Re: Recursive function

2013-03-05 Thread Vlastimil Brom
2013/3/5 Ana Dionísio : > Hello! > > I have to make a script that calculates temperature, but one of the > parameters is the temperature in the iteration before, for example: > temp = (temp_-1)+1 > > it = 0 > temp = 3 > > it = 1 > temp = 3+1 > > it = 2 > temp = 4+1 > > How can I do this in a simple

Re: Recursive function

2013-03-05 Thread Dave Angel
On 03/05/2013 10:32 AM, Ana Dionísio wrote: Hello! I have to make a script that calculates temperature, but one of the parameters is the temperature in the iteration before, for example: temp = (temp_-1)+1 it = 0 temp = 3 it = 1 temp = 3+1 it = 2 temp = 4+1 How can I do this in a simple way?

Re: Recursive function won't compile

2008-04-02 Thread Keith Thompson
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes: >> #include >> #include >> >> def RecursiveFact(n): >> if(n>1): >> return n*RecursiveFact(n-1) >> else: >> return 1 >> >> fact = RecursiveFact(31) >> print fact >> >> fact = "End of program" >> print fact >> >> >> ..but y

Re: Recursive function won't compile

2008-04-02 Thread George Sakkis
On Apr 2, 5:00 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] schrieb: > > > > > #include > > #include > > > def RecursiveFact(n): > > if(n>1): > > return n*RecursiveFact(n-1) > > else: > > return 1 > > > fact = RecursiveFact(31) > > print fact > > >

Re: Recursive function won't compile

2008-04-02 Thread Diez B. Roggisch
[EMAIL PROTECTED] schrieb: > #include > #include > > def RecursiveFact(n): > if(n>1): > return n*RecursiveFact(n-1) > else: > return 1 > > fact = RecursiveFact(31) > print fact > > fact = "End of program" > print fact > > > ..but yet it still gives the right answe

Re: Recursive function won't compile

2008-04-02 Thread dj3vande
In article <[EMAIL PROTECTED]>, <[EMAIL PROTECTED]> wrote: (Subject: Recursive function won't compile) >#include >#include > >def RecursiveFact(n): >..but yet it still gives the right answer. How is this possible? Possibly because it gives the right answer in a different language than it

Re: Recursive function won't compile

2008-04-02 Thread ajaksu
On Apr 2, 5:23 pm, [EMAIL PROTECTED] wrote: > #include > #include > > def RecursiveFact(n): >     if(n>1): >         return n*RecursiveFact(n-1) >     else: >         return 1 > > fact = RecursiveFact(31) > print fact The output is 822283865417792281772556288000 and is correct. But the "#incl

Re: Recursive function won't compile

2008-04-02 Thread Gary Herron
[EMAIL PROTECTED] wrote: > #include > #include > > def RecursiveFact(n): > if(n>1): > return n*RecursiveFact(n-1) > else: > return 1 > > fact = RecursiveFact(31) > print fact > > fact = "End of program" > print fact > > > ..but yet it still gives the right answer. How

Re: recursive function

2007-01-09 Thread cesco
Hendrik van Rooyen wrote: > "cesco" <[EMAIL PROTECTED]> wrote: > > > > > Neil Cerutti wrote: > > > On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote: > > > > Hi, > > > > > > > > I have a dictionary of lists of tuples like in the following example: > > > > dict = {1: [(3, 4), (5, 8)], > > > >

Re: recursive function

2007-01-09 Thread Hendrik van Rooyen
"cesco" <[EMAIL PROTECTED]> wrote: > > Neil Cerutti wrote: > > On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote: > > > Hi, > > > > > > I have a dictionary of lists of tuples like in the following example: > > > dict = {1: [(3, 4), (5, 8)], > > > 2: [(5, 4), (21, 3), (19, 2)], > > >

Re: recursive function

2007-01-08 Thread Max Erickson
"cesco" <[EMAIL PROTECTED]> wrote: > Hi, > > I have a dictionary of lists of tuples like in the following > example: dict = {1: [(3, 4), (5, 8)], > 2: [(5, 4), (21, 3), (19, 2)], > 3: [(16, 1), (0, 2), (1, 2), (3, 4)]] > > In this case I have three lists inside the dict but this

Re: recursive function

2007-01-08 Thread Tim Williams
On 8 Jan 2007 16:03:53 +0100, Neil Cerutti <[EMAIL PROTECTED]> wrote: > > len(dict.keys()). > Or len(dict) :) -- http://mail.python.org/mailman/listinfo/python-list

Re: recursive function

2007-01-08 Thread bearophileHUGS
First possible solution: def rloop(seqin, comb): # xcross product recipe 302478 by David Klaffenbach if seqin: for item in seqin[0]: newcomb = comb + [item] for item in rloop(seqin[1:], newcomb): yield item else: yield comb data

Re: recursive function

2007-01-08 Thread cesco
Neil Cerutti wrote: > On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote: > > Hi, > > > > I have a dictionary of lists of tuples like in the following example: > > dict = {1: [(3, 4), (5, 8)], > > 2: [(5, 4), (21, 3), (19, 2)], > > 3: [(16, 1), (0, 2), (1, 2), (3, 4)]] > > > > In this

Re: recursive function

2007-01-08 Thread Neil Cerutti
On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote: > Hi, > > I have a dictionary of lists of tuples like in the following example: > dict = {1: [(3, 4), (5, 8)], > 2: [(5, 4), (21, 3), (19, 2)], > 3: [(16, 1), (0, 2), (1, 2), (3, 4)]] > > In this case I have three lists inside the dict

Re: Recursive function returning a list

2006-07-20 Thread Bruno Desthuilliers
[EMAIL PROTECTED] wrote: > Bruno Desthuilliers wrote: > >>[EMAIL PROTECTED] wrote: > > [...] > >>>Sorry, but I kinda agree with Boris here. >> >>On what ? > > > On the argument that you are (implicitly?) disagreeing with him it's getting messy - too much level of indirection !-) > on, > obv

Re: Recursive function returning a list

2006-07-19 Thread malkarouri
Bruno Desthuilliers wrote: > [EMAIL PROTECTED] wrote: [...] > > Sorry, but I kinda agree with Boris here. > > On what ? On the argument that you are (implicitly?) disagreeing with him on, obviously. That the OP problem is not definitely the default values question. As you say: > >>If the OP has o

Re: Recursive function returning a list

2006-07-19 Thread Boris Borcic
Bruno Desthuilliers wrote: > > Nope, it's about trying to make sure that anyone googling for a similar > problem will notice the canonical solution somehow. > Hey, I challenge you to cook up a plausible query even loosely fitting your "googling for a similar problem" that would return my answe

Re: Recursive function returning a list

2006-07-19 Thread Bruno Desthuilliers
Steve Holden wrote: > Bruno Desthuilliers wrote: > >> Boris Borcic a écrit : >> >>> Hello Bruno, >>> >>> Bruno Desthuilliers wrote: > > [...] > Or how to *not* address the real problem... Boris, using a generator may be a pretty good idea, but *not* as a way to solve a proble

Re: Recursive function returning a list

2006-07-19 Thread Bruno Desthuilliers
[EMAIL PROTECTED] wrote: > Bruno Desthuilliers wrote: > >>Boris Borcic a écrit : >> >>>Hello Bruno, >>> >>>Bruno Desthuilliers wrote: >>> >>> Boris Borcic wrote: >>Do you have any ideas? > > >you could use a recursive generator, like > >def genAllChildren(self

Re: Recursive function returning a list

2006-07-19 Thread Boris Borcic
Bruno Desthuilliers wrote: > Boris Borcic a écrit : >> Hello Bruno, >> >> Bruno Desthuilliers wrote: >> >>> Boris Borcic wrote: >>> > Do you have any ideas? you could use a recursive generator, like def genAllChildren(self) : for child in self.children :

Re: Recursive function returning a list

2006-07-19 Thread Steve Holden
Bruno Desthuilliers wrote: > Boris Borcic a écrit : > >>Hello Bruno, >> >>Bruno Desthuilliers wrote: [...] >>>Or how to *not* address the real problem... >>> >>>Boris, using a generator may be a pretty good idea, but *not* as a way >>>to solve a problem that happens to be a FAQ !-) >>> >> >>Sorry,

Re: Recursive function returning a list

2006-07-18 Thread malkarouri
Bruno Desthuilliers wrote: > Boris Borcic a écrit : > > Hello Bruno, > > > > Bruno Desthuilliers wrote: > > > >> Boris Borcic wrote: > >> > Do you have any ideas? > >>> > >>> > >>> you could use a recursive generator, like > >>> > >>> def genAllChildren(self) : > >>> for child in self.chil

Re: Recursive function returning a list

2006-07-18 Thread Bruno Desthuilliers
Boris Borcic a écrit : > Hello Bruno, > > Bruno Desthuilliers wrote: > >> Boris Borcic wrote: >> Do you have any ideas? >>> >>> >>> you could use a recursive generator, like >>> >>> def genAllChildren(self) : >>> for child in self.children : >>> yield child >>> for childc

Re: Recursive function returning a list

2006-07-18 Thread Boris Borcic
Hello Bruno, Bruno Desthuilliers wrote: > Boris Borcic wrote: >>> Do you have any ideas? >> >> you could use a recursive generator, like >> >> def genAllChildren(self) : >> for child in self.children : >> yield child >> for childchild in child.genAllChildren() : >>

Re: Recursive function returning a list

2006-07-18 Thread Bruno Desthuilliers
Boris Borcic wrote: >> Do you have any ideas? > > > you could use a recursive generator, like > > def genAllChildren(self) : > for child in self.children : > yield child > for childchild in child.genAllChildren() : > yield childchild Or how to *not* address the

Re: Recursive function returning a list

2006-07-18 Thread Boris Borcic
> Do you have any ideas? you could use a recursive generator, like def genAllChildren(self) : for child in self.children : yield child for childchild in child.genAllChildren() : yield childchild -- http://mail.python.org/mailman/listinfo/python-list

Re: Recursive function returning a list

2006-07-17 Thread Steve Holden
Fabian Steiner wrote: > Hello! > > I have got a Python "Device" Object which has got a attribute (list) > called children which my contain several other "Device" objects. I > implemented it this way in order to achieve a kind of parent/child > relationship. > > Now I would like to get all chil

Re: Recursive function returning a list

2006-07-17 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Fabian Steiner wrote: > This is what I got so far: > > def getAllChildren(self, children=[]): > if self.children: > children.extend(self.children) > for child in self.children: > child.getAllChildren(children) >

Re: Recursive function going infinite and I can't see why.

2006-02-04 Thread Gregory Piñero
Ok, I finally got it working! See below On 2/4/06, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Sat, 04 Feb 2006 02:18:27 -0500, Gregory Piñero wrote: > > class Node: > > def __init__(self): > > self.arg0=0 > > self.arg1=0 > > self.arg2=0 > > self.arg3=0 > >

Re: Recursive function going infinite and I can't see why.

2006-02-04 Thread Scott David Daniels
Gregory Piñero wrote: > I want to walk down each tree and get a random subtree at a random > depth. Can you quantify that randomness? Should it be uniform at each level? Thinking about this may be fruitful. I don't yet know whether you need to see all leaves before you know which subtree

Re: Recursive function going infinite and I can't see why.

2006-02-04 Thread Gregory Piñero
Thanks for the advice guys. See below. On 2/4/06, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Sat, 04 Feb 2006 02:18:27 -0500, Gregory Piñero wrote: > > > class Node: > > def __init__(self): > > self.arg0=0 > > self.arg1=0 > > self.arg2=0 > > self.arg3=0 >

Re: Recursive function going infinite and I can't see why.

2006-02-04 Thread Steven D'Aprano
On Sat, 04 Feb 2006 02:18:27 -0500, Gregory Piñero wrote: > Hi, > > Would anyone be able to tell me why my function below is getting stuck > in infinite recusion? > Maybe I'm just tired and missing something obvious? Your code is quite confusing, especially since there is very little documentati

Re: Recursive function going infinite and I can't see why.

2006-02-04 Thread Terry Reedy
>Would anyone be able to tell me why my function below is getting stuck >in infinite recusion? >def replace_within_node(node,oldnode,newnode): >if node is oldnode: >return newnode Without looking further, the most likely reason is that the base case is never true: ie, node is never o

Re: Recursive function going infinite and I can't see why.

2006-02-03 Thread Gregory Piñero
By the way, all I'm trying to do here is take two trees, randomly find a sub-tree of each and swap the sub-trees. So if anyone has a simple method for doing that I'm certainly open to that too. Thanks again, -Greg On 2/4/06, Gregory Piñero <[EMAIL PROTECTED]> wrote: > Hi, > > Would anyone be a

Re: recursive function return value problems

2005-12-28 Thread bonono
[EMAIL PROTECTED] wrote: > hi, i have the following recursive function (simplified to demonstrate > the problem): > > >>> def reTest(bool): > ... result = [] > ... if not bool: > ... reTest(True) > ... else: > ... print "YAHHH" > ... result = ["should be the onl

Re: recursive function return value problems

2005-12-28 Thread Steve Holden
[EMAIL PROTECTED] wrote: > hi, i have the following recursive function (simplified to demonstrate > the problem): > > def reTest(bool): > > ... result = [] > ... if not bool: > ... reTest(True) > ... else: > ... print "YAHHH" > ... result = ["should be the

Re: recursive function return value problems

2005-12-28 Thread Mike Meyer
[EMAIL PROTECTED] writes: > hi, i have the following recursive function (simplified to demonstrate > the problem): def reTest(bool): > ... result = [] > ... if not bool: > ... reTest(True) > ... else: > ... print "YAHHH" > ... result = ["should be the only t

Re: recursive function return value problems

2005-12-28 Thread Martin v. Löwis
[EMAIL PROTECTED] wrote: > ... if not bool: > ... reTest(True) > I don't understand why results are returned twice? is there something > special i missed about recursive functions? Yes, although it is not clear *what* it is that you are missing. Here is my guess: you fail to see that

Re: recursive function return value problems

2005-12-28 Thread Steven D'Aprano
On Wed, 28 Dec 2005 16:05:30 -0800, randomtalk wrote: > the final returned value is: [] > > the two values printed is (note i only have one print statement > printing "print result",. however, in the actualality, it's printed > twice): > printing result: > ['should be the only thing returned'] >

Re: recursive function return value problems

2005-12-28 Thread randomtalk
ah, result = reTest(True) works, thanks alot :D -- http://mail.python.org/mailman/listinfo/python-list

Re: recursive function return value problems

2005-12-28 Thread Steven D'Aprano
On Wed, 28 Dec 2005 15:25:30 -0800, randomtalk wrote: > hi, i have the following recursive function (simplified to demonstrate > the problem): > def reTest(bool): > ... result = [] > ... if not bool: > ... reTest(True) > ... else: > ... print "YAHHH" > ...

Re: recursive function return value problems

2005-12-28 Thread casevh
You have two calls to reTest and so reTest needs to return twice. One return is from the reTest(True) call back to reTest(False). The second return is from reTest(False) back to the prompt. What were you expecting to happen? -- http://mail.python.org/mailman/listinfo/python-list

Re: recursive function return value problems

2005-12-28 Thread Dan Sommers
On 28 Dec 2005 15:25:30 -0800, [EMAIL PROTECTED] wrote: > hi, i have the following recursive function (simplified to demonstrate > the problem): def reTest(bool): > ... result = [] > ... if not bool: > ... reTest(True) Don't do that. Do this instead: result =

Re: recursive function return value problems

2005-12-28 Thread randomtalk
the final returned value is: [] the two values printed is (note i only have one print statement printing "print result",. however, in the actualality, it's printed twice): printing result: ['should be the only thing returned'] printing result: [] therefore, sadly, i don't thinkg you've understand

Re: recursive function return value problems

2005-12-28 Thread Brian L. Troutwine
[EMAIL PROTECTED] wrote: def reTest(bool): > > ... result = [] > ... if not bool: > ... reTest(True) > ... else: > ... print "YAHHH" > ... result = ["should be the only thing returned"] > ... print "printing result: " > ... print result > ... return re

Re: recursive function call

2005-11-08 Thread bruno at modulix
Nicolas Vigier wrote: > Hello, > > I have in my python script a function that look like this : > > def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): > if type(arg1) is ListType: How should it behave with tuples or subclasses of List ? Or if it's any other iterable ? Testing against

Re: recursive function call

2005-11-08 Thread Nicolas Vigier
Peter Otten said: > Here is a non-recursive approach: > > def tolist(arg): >if isinstance(arg, list): >return arg >return [arg] > > def f(arg1, arg2, more_args): >for arg1 in tolist(arg1): >for arg2 in tolist(arg2): ># real code > > If it were my code I would

Re: recursive function call

2005-11-08 Thread Duncan Booth
Nicolas Vigier wrote: > I have in my python script a function that look like this : > > def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): > if type(arg1) is ListType: > for a in arg1: > my_function(a, arg2, opt1=opt1, opt2=opt2, >

Re: recursive function call

2005-11-08 Thread Peter Otten
Nicolas Vigier wrote: > Hello, > > I have in my python script a function that look like this : > > def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): > if type(arg1) is ListType: > for a in arg1: > my_function(a, arg2, opt1=opt1, opt2=opt2, >

Re: recursive function

2005-10-07 Thread Brandon K
> def foo(j): > while j < n: > j+=1 > return j > of course I mean: def foo(j): while j < n: j+=1 return j sorry == Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups == Get Anonymous, Uncensored, Access to West and East Coast Server Fa

Re: recursive function

2005-10-07 Thread Brandon K
Is there no way to implement your idea in a classical loop? Usually the syntax is cleaner, and there is no limit (except the limit of the range function in certain cases). For example what would be wrong with. def foo(j): while j < n: j+=1 return j I don't know much about th

Re: recursive function

2005-10-07 Thread Juho Schultz
mg wrote: > Hello, > > In a recursive function like the following : > > > def foo( j ) : > j += 1 > while j < n : j = foo( j ) > return j > > > in found that the recursivity is limited (1000 iterations). Then, I have > two questions : > - why this mecanism has been implemented ? > - it is