Re: Top level of a recursive function

2022-12-17 Thread Rob Cliffe via Python-list
On 14/12/2022 13:49, Stefan Ram wrote: I also found an example similar to what was discussed here in pypy's library file "...\Lib\_tkinter\__init__.py": |def _flatten(item): |def _flatten1(output, item, depth): |if depth > 1000: |raise ValueError("nesting too deep

Re: Top level of a recursive function

2022-12-15 Thread Peter Otten
On 13/12/2022 15:46, Michael F. Stemper wrote: It's easy enough -- in fact necessary -- to handle the bottom level of a function differently than the levels above it. What about the case where you want to handle something differently in the top level than in lower levels? Is there any way to

Re: Top level of a recursive function

2022-12-13 Thread Michael F. Stemper
On 13/12/2022 09.03, Stefan Ram wrote: "Michael F. Stemper" writes: def fred(cf,toplevel=True): x = cf[0] if len(cf)>1: if toplevel: return x + fred(cf[1:],False) else: return "(" + x + fred(cf[1:],False) + ")" else: if toplevel: return x else:

Re: Top level of a recursive function

2022-12-13 Thread Chris Angelico
On Wed, 14 Dec 2022 at 05:01, Mats Wichmann wrote: > > On 12/13/22 10:36, Chris Angelico wrote: > > On Wed, 14 Dec 2022 at 03:35, Michael F. Stemper > > wrote: > >> > >> It's easy enough -- in fact necessary -- to handle the bottom > >> level of a function differently than the levels above it.

Re: Top level of a recursive function

2022-12-13 Thread Mats Wichmann
On 12/13/22 10:36, Chris Angelico wrote: On Wed, 14 Dec 2022 at 03:35, Michael F. Stemper wrote: It's easy enough -- in fact necessary -- to handle the bottom level of a function differently than the levels above it. What about the case where you want to handle something differently in the

Re: Top level of a recursive function

2022-12-13 Thread Chris Angelico
On Wed, 14 Dec 2022 at 03:35, Michael F. Stemper wrote: > > It's easy enough -- in fact necessary -- to handle the bottom > level of a function differently than the levels above it. What > about the case where you want to handle something differently > in the top level than in lower levels? Is

RE: Top level of a recursive function

2022-12-13 Thread Schachner, Joseph (US)
: Tuesday, December 13, 2022 10:25 AM To: python-list@python.org Subject: Re: Top level of a recursive function Supersedes: r...@zedat.fu-berlin.de (Stefan Ram) writes: >def rest( s ): >return "(" + s[ 0 ] +( rest( s[1:] ) if len( s )> 1 else '' )+ ')' >def nest( s ): &g

Top level of a recursive function

2022-12-13 Thread Michael F. Stemper
It's easy enough -- in fact necessary -- to handle the bottom level of a function differently than the levels above it. What about the case where you want to handle something differently in the top level than in lower levels? Is there any way to tell from within a function that it wasn't invoked

[issue43187] Dict creation in recursive function cause interpreter crashes.

2021-09-07 Thread Irit Katriel
Irit Katriel added the comment: On 3.9 I reproduce the segfault. On 3.11 on a mac I get RecursionError: maximum recursion depth exceeded while calling a Python object So it has been fixed in the meantime. -- nosy: +iritkatriel resolution: -> out of date stage: -> resolved

[issue43187] Dict creation in recursive function cause interpreter crashes.

2021-02-10 Thread Yang Feng
creation in recursive function cause interpreter crashes. type: crash versions: Python 3.9 ___ Python tracker <https://bugs.python.org/issue43187> ___ ___ Python-bugs-list m

[issue4921] Object lifetime and inner recursive function

2018-09-11 Thread Eric Snow
Change by Eric Snow : -- nosy: +eric.snow ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue4921] Object lifetime and inner recursive function

2018-09-10 Thread Eric Wieser
Eric Wieser added the comment: For anyone doing archaeology - this came up on python-dev about a year after this issue was filed: https://mail.python.org/pipermail/python-dev/2009-December/094439.html -- ___ Python tracker

[issue4921] Object lifetime and inner recursive function

2018-02-19 Thread Eric Wieser
Eric Wieser added the comment: Would it be possible for function self-reference cell vars to be weak references? This wouldn't solve the issue for co-recursive inner functions, but would at least prevent reference cycles for the more common case of simple

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

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 steve+comp.lang.pyt...@pearwood.info 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

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 jtim.arn...@gmail.com 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

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 alb...@spenarnc.xs4all.nl 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

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: 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

recursive function: use a global or pass a parameter?

2015-01-16 Thread Tim
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 beforehand how

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 jtim.arn...@gmail.com 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):

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): d =

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 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): ... if not

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

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 greg.ew...@canterbury.ac.nz 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

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(): ... yield

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:

Recursive function

2013-03-05 Thread 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 way? Thanks a lot! --

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

Re: Recursive function

2013-03-05 Thread Vlastimil Brom
2013/3/5 Ana Dionísio anadionisio...@gmail.com: 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

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 Neil Cerutti
is to first write the simplest version of the function you can think of, the version that solves the most trivial form for the problem. For example, lets say I want to write a recursive function to reverse a string (you would never do this except as an exercise), I'd first write a version that can

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Yuval Greenfield
. -- components: Library (Lib) files: rglob.patch keywords: patch messages: 152843 nosy: ubershmekel priority: normal severity: normal status: open title: Add a recursive function to the glob package type: enhancement versions: Python 3.4 Added file: http://bugs.python.org/file24451/rglob.patch

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Giampaolo Rodola'
Changes by Giampaolo Rodola' g.rod...@gmail.com: -- nosy: +giampaolo.rodola ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13968 ___ ___

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Nick Coghlan
Nick Coghlan ncogh...@gmail.com added the comment: I'm inclined to close this as a functional duplicate of http://bugs.python.org/issue13229 -- nosy: +ncoghlan ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13968

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Yuval Greenfield
Yuval Greenfield ubershme...@gmail.com added the comment: I'd say it's very close to a duplicate but maybe isn't so. If walkdir is added then rglob can be implemented using it. I'd say rglob to walkdir is like urlopen to http.client. One is the stupid and simple function (that still has a

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Antoine Pitrou
Antoine Pitrou pit...@free.fr added the comment: file_paths(filtered_walk('.', included_files=['*.py'])) is a lot longer than rglob('*.py'). Agreed. -- nosy: +pitrou ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13968

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Nick Coghlan
Nick Coghlan ncogh...@gmail.com added the comment: A fair point indeed. To follow the shutil naming convention (rmtree, copytree, and likely chmodtree, chowntree), a more appropriate name might be globtree. (Thanks to string methods, the 'r' prefix doesn't read correctly to me: what does

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Antoine Pitrou
Antoine Pitrou pit...@free.fr added the comment: To follow the shutil naming convention (rmtree, copytree, and likely chmodtree, chowntree), a more appropriate name might be globtree. (Thanks to string methods, the 'r' prefix doesn't read correctly to me: what does globbing from the right

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Nick Coghlan
Nick Coghlan ncogh...@gmail.com added the comment: I can live with it either way - I just wanted to point out that our current examples of this kind of recursive filesystem access use a 'tree' suffix rather than an 'r' prefix. -- ___ Python tracker

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Eli Bendersky
Eli Bendersky eli...@gmail.com added the comment: file_paths(filtered_walk('.', included_files=['*.py'])) is a lot longer than rglob('*.py'). It is, but is that a good enough reason to have both? It can also be achieved with just a bit more code using the simple `os.walk`. I suppose there

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Antoine Pitrou
Antoine Pitrou pit...@free.fr added the comment: file_paths(filtered_walk('.', included_files=['*.py'])) is a lot longer than rglob('*.py'). It is, but is that a good enough reason to have both? It is. globbing is a well-known operation that many people expect to be easily done.

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Éric Araujo
Éric Araujo mer...@netwok.org added the comment: There is an alternative: supporting ** syntax, e.g. '**/*.py', which should find all *.py files in the current directory and all descendents. At present glob('**/*.py') is equivalent to glob('*/*.py'), but we would say this behavior was

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Eli Bendersky
Eli Bendersky eli...@gmail.com added the comment: It is. globbing is a well-known operation that many people expect to be easily done. According to Wikipedia (http://en.wikipedia.org/wiki/Glob_%28programming%29) - The noun glob is used to refer to a particular pattern, e.g. use the glob

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Antoine Pitrou
Antoine Pitrou pit...@free.fr added the comment: IOW, globbing is usually understood as the act of expanding a pattern to the files it matches. Nothing in that implies recursive traversal of a directory tree. Still, that's a common need. I want all Python files in a subtree. On the other

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Eli Bendersky
and awkward, it should probably be improved *now* while it's in development. Adding yet another tool that implements part of its functionality, winning a golf tournament along the way, isn't the solution, IMHO. -- title: Support recursive globs - Add a recursive function to the glob

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Éric Araujo
Éric Araujo mer...@netwok.org added the comment: Feedback from Antoine on IRC about my syntax proposal: “The ** meaning is not really universal like other quantifiers are. [...] (also, it would be quite harder to implement, I think)” That and the compat issue makes me go in favor of a new

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Antoine Pitrou
Antoine Pitrou pit...@free.fr added the comment: Google walk directory. First hit is a Rosetta code page with *recursive* walking implemented in various languages. So I guess it does have this connotation. Regardless, os.walk has been in Python for ages, and it's always been the go-to tool

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Eli Bendersky
Eli Bendersky eli...@gmail.com added the comment: Google walk directory. First hit is a Rosetta code page with *recursive* walking implemented in various languages. So I guess it does have this connotation. Regardless, os.walk has been in Python for ages, and it's always been the go-to tool

[issue13968] Add a recursive function to the glob package

2012-02-08 Thread Antoine Pitrou
Antoine Pitrou pit...@free.fr added the comment: I'm trying the patch and its behaviour is strange: list(glob.rglob('setup.py')) ['setup.py'] list(glob.rglob('setu*.py')) [] list(glob.rglob('*/setu*.py')) ['./setup.py', './Mac/Tools/Doc/setup.py', './Tools/test2to3/setup.py',

Re: Hotshoting recursive function

2011-05-26 Thread Selvam
your function should call the undecorated function, not the decorated function again. Decorator syntax is not convenient anymore. Using the same names as in the recipe example: # a recursive function def my_slow_function(n): ... return my_slow_function(n-1) my_profiled_slow_function

Re: Hotshoting recursive function

2011-05-25 Thread Gabriel Genellina
. Decorator syntax is not convenient anymore. Using the same names as in the recipe example: # a recursive function def my_slow_function(n): ... return my_slow_function(n-1) my_profiled_slow_function = hotshotit(my_slow_function) my_profiled_slow_function(n) This works, in the sense

Hotshoting recursive function

2011-05-22 Thread Selvam
Hi, I am using hotshot module to profile my python function. I used the details from ( http://code.activestate.com/recipes/576656-quick-python-profiling-with-hotshot/ ). The function I profile is a recursive one and I am getting the following error, ProfilerError: profiler already active I

Re: list parameter of a recursive function

2010-10-07 Thread Seebs
On 2010-10-07, TP tribulati...@paralleles.invalid wrote: Diez B. Roggisch wrote: A safer alternative for these cases is using tuples, because they are immutable. The problem with tuples is that it is not easy to modify them: This is probably the best post-and-response I've seen in the last

Re: list parameter of a recursive function

2010-10-07 Thread Chris Rebert
On Wed, Oct 6, 2010 at 10:25 PM, TP tribulati...@paralleles.invalid wrote: Diez B. Roggisch wrote: Back to your example: your solution is perfectly fine, although a bit costly and more error-prone if you happen to forget to create a copy. A safer alternative for these cases is using tuples,

Re: list parameter of a recursive function

2010-10-07 Thread Diez B. Roggisch
TP tribulati...@paralleles.invalid writes: Diez B. Roggisch wrote: Back to your example: your solution is perfectly fine, although a bit costly and more error-prone if you happen to forget to create a copy. A safer alternative for these cases is using tuples, because they are immutable.

Re: list parameter of a recursive function

2010-10-07 Thread Terry Reedy
On 10/6/2010 3:22 PM, TP wrote: Hi, I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) This sort of function is an exception.

Re: list parameter of a recursive function

2010-10-07 Thread TP
Seebs wrote: On 2010-10-07, TP tribulati...@paralleles.invalid wrote: Diez B. Roggisch wrote: A safer alternative for these cases is using tuples, because they are immutable. The problem with tuples is that it is not easy to modify them: This is probably the best post-and-response I've

list parameter of a recursive function

2010-10-06 Thread TP
Hi, I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) This is the outline of my function: def f ( argument, some_list = None

Re: list parameter of a recursive function

2010-10-06 Thread Chris Torek
In article rsuun7-eml@rama.universe TP tribulati...@paralleles.invalid wrote: I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at:

Re: list parameter of a recursive function

2010-10-06 Thread TP
Chris Torek wrote: import copy from copy [from copy import copy, rather] Yes, sorry. Note that if f() is *supposed* to be able to modify its second parameter under some conditions, you would want to make the copy not at the top of f() but rather further in, and in this case, that would

Re: list parameter of a recursive function

2010-10-06 Thread Diez B. Roggisch
TP tribulati...@paralleles.invalid writes: Hi, I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) This is the outline of

Re: list parameter of a recursive function

2010-10-06 Thread Steven D'Aprano
On Wed, 06 Oct 2010 23:00:10 +0200, TP wrote: I think I prefer doing an explicit copy.copy, because it allows to remind the reader that it is very important to take care of that. I think a comment is better for that. It's better to explicitly say something is important than to assume the

Re: list parameter of a recursive function

2010-10-06 Thread TP
Diez B. Roggisch wrote: Back to your example: your solution is perfectly fine, although a bit costly and more error-prone if you happen to forget to create a copy. A safer alternative for these cases is using tuples, because they are immutable. Thanks Diez for your explanation. The problem

Re: list parameter of a recursive function

2010-10-06 Thread TP
Steven D'Aprano wrote: I think I prefer doing an explicit copy.copy, because it allows to remind the reader that it is very important to take care of that. I think a comment is better for that. It's better to explicitly say something is important than to assume the reader will guess. Yes,

Re: it doesn't work ;) [class recursive function]

2010-09-17 Thread MRAB
On 17/09/2010 17:55, Ethan Furman wrote: MRAB wrote: On 16/09/2010 00:23, Ethan Furman wrote: I need some fresh eyes, or better brains, or both! 'next_item' is a generator, but it's just calling itself and discarding the result. I think it should be yielding the results to its caller. That

Re: it doesn't work ;) [class recursive function]

2010-09-17 Thread Ethan Furman
MRAB wrote: On 17/09/2010 17:55, Ethan Furman wrote: MRAB wrote: On 16/09/2010 00:23, Ethan Furman wrote: PS My apologies if this shows up twice, I haven't seen my other post yet and it's been 27 hours. That's probably because you sent it directly to me. That would explain it -- like I

it doesn't work ;) [class recursive function]

2010-09-15 Thread Ethan Furman
I need some fresh eyes, or better brains, or both! The expected debugging output is a list of names in alphabetical order from each node (there are about 90 of them); what I am getting is this: -- dbf.tables.Index.from_file('', r'aad13658_last_name_for_state.idx') starting next_item call

Re: it doesn't work ;) [class recursive function]

2010-09-15 Thread MRAB
On 16/09/2010 00:23, Ethan Furman wrote: I need some fresh eyes, or better brains, or both! The expected debugging output is a list of names in alphabetical order from each node (there are about 90 of them); what I am getting is this: -- dbf.tables.Index.from_file('',

my recursive function call is wrong?

2009-08-16 Thread Chang Min Jeon
I'm trying to write program to translate define macro in 'C'. And start_parse has return condition that list's length is 0. At this time return statement invoke start_parse() function. I can't understand do that. I'm using Python 2.6.2 in Windows XP import re import sys comment = ''' #if defined

Re: my recursive function call is wrong?

2009-08-16 Thread Kev Dwyer
On Sun, 16 Aug 2009 16:57:41 +0900, Chang Min Jeon wrote: Hello, You have placed recursive calls to the function in a number of different locations; when len(macro) becomes zero control will return to the calling function, but this calling function may have more code to execute, including

Re: my recursive function call is wrong?

2009-08-16 Thread Chang Min Jeon
Dear Kev Thank you very much. I got it.:) 2009/8/16 Kev Dwyer kevin.p.dw...@gmail.com On Sun, 16 Aug 2009 16:57:41 +0900, Chang Min Jeon wrote: Hello, You have placed recursive calls to the function in a number of different locations; when len(macro) becomes zero control will return to

Re: Puzzling: local variable in recursive function made global?

2009-04-02 Thread Aahz
In article mailman.2724.1238086228.11746.python-l...@python.org, andrew cooke and...@acooke.org wrote: sorry for the shouting, but someone asks this EVERY DAY AND I CAN'T TAKE ANY MORE. Nobody's forcing you to respond. Nobody's forcing you to top-post, either. -- Aahz (a...@pythoncraft.com)

Puzzling: local variable in recursive function made global?

2009-03-26 Thread Daniel Oberski
)) c1.add(Branch(next = c2)) c2.add(Branch(next = b1)) print a.is_terminal() 1 # Total number of times the recursive function is_terminal has been called: num_calls = 0 class Node: def __init__(self, name): self.name = name self.branches = [] def add(self, branch

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread andrew cooke
)) a.add(Branch(next = c1)) c1.add(Branch(next = c2)) c2.add(Branch(next = b1)) print a.is_terminal() 1 # Total number of times the recursive function is_terminal has been called: num_calls = 0 class Node: def __init__(self, name): self.name = name self.branches

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Diez B. Roggisch
Daniel Oberski schrieb: Hello all, I wrote this function to recurse through a tree structure of Nodes connected by Branches. I use a local variable seen_nodes to mark off Nodes already seen by the function (i.e. add them to a list). That's not a local variable, that is a default

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread andrew cooke
Diez B. Roggisch wrote: That's not a local variable, that is a default argument. Which is in fact only created once for each function, yes. http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm a nice way of handling this was posted here just yesterday, which isn't in the

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Peter Otten
andrew cooke wrote: Diez B. Roggisch wrote: That's not a local variable, that is a default argument. Which is in fact only created once for each function, yes. http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm a nice way of handling this was posted here just

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Daniel Oberski
Hi Andrew, it's not global, it's mutable. you are passing THE SAME FRICKING OBJECT to is_terminal and appending values to it. That, I understand. I already saw in the archives this confuses many people, e.g. the thread Odd behavior regarding a list. I think this is understandable if you

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Daniel Oberski
Hi Diez, Great, this totally clears it up. Thank you! - daniel On Thu, 26 Mar 2009 17:50:20 +0100, Diez B. Roggisch wrote: That's not a local variable, that is a default argument. Which is in fact only created once for each function, yes.

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Daniel Oberski
Hi Peter, Plus, it works as expected (read: modifies the argument) if you explicitly pass an empty list to the function... That is not so. The reason is given by Andrew Cooke in this thread. I would expect that when function calls lower in the recursion hierarchy return, the object is not

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread MRAB
andrew cooke wrote: it's not global, it's mutable. you are passing THE SAME FRICKING OBJECT to is_terminal and appending values to it. instead, make a new copy: branch.next.is_terminal(list(seen_nodes)) sorry for the shouting, but someone asks this EVERY DAY AND I CAN'T TAKE ANY MORE.

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Peter Otten
Daniel Oberski wrote: Hi Peter, Plus, it works as expected (read: modifies the argument) if you explicitly pass an empty list to the function... That is not so. The reason is given by Andrew Cooke in this thread. I would expect that when function calls lower in the recursion hierarchy

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Terry Reedy
If the order of node-entry into seen_nodes is never used (if particular, if 'node in seen_nodes' is its only usage), then seen_nodes could be a set and the 'in' operation would be O(1) instead of O(n). -- http://mail.python.org/mailman/listinfo/python-list

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread andrew cooke
Peter Otten wrote: I would be surprised by the behaviour of Andrew's variant: def append(item, items=None): ... items = items or [] ... items.append(item) ... return items ... a = [] append(x, a) ['x'] ah that's a good point. andrew --

[issue4921] Object lifetime and inner recursive function

2009-01-12 Thread Hirokazu Yamamoto
and inner recursive function versions: Python 2.6, Python 2.7, Python 3.0, Python 3.1 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue4921 ___ ___ Python-bugs-list

[issue4921] Object lifetime and inner recursive function

2009-01-12 Thread Antoine Pitrou
Antoine Pitrou pit...@free.fr added the comment: Since g calls itself in its own scope, it is stored as one of its own cell vars, which creates a reference cycle. a is also part of its reference cycle for the same reason, so it must wait for garbage collection to be reclaimed. If g didn't keep

[issue4921] Object lifetime and inner recursive function

2009-01-12 Thread Hirokazu Yamamoto
Hirokazu Yamamoto ocean-c...@m2.ccsnet.ne.jp added the comment: Thank you for explanation. The combination of inner function + method variable was very handy for me, but maybe I should refrain from using it lightly. :-( -- resolution: - invalid status: open - closed

[issue4921] Object lifetime and inner recursive function

2009-01-12 Thread Hirokazu Yamamoto
Hirokazu Yamamoto ocean-c...@m2.ccsnet.ne.jp added the comment: A little followup. Attached script q112.py (some puzzle program) ate up my RAM (80MB) and never ran GC while solving. Current python GC looks at the number of GC objects, but GC object like set object can contain many non GC

[issue4921] Object lifetime and inner recursive function

2009-01-12 Thread Antoine Pitrou
Antoine Pitrou pit...@free.fr added the comment: Well, tracking memory consumption of each container would be better than simpling couting them, but it's much more complicated as well (not to mention that memory consumption can vary, so you must recalculate it periodically...).

Re: Pass same parameter in Recursive function

2008-09-03 Thread Diez B. Roggisch
, 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

Pass same parameter in Recursive function

2008-09-02 Thread Davy
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

Re: Pass same parameter in Recursive function

2008-09-02 Thread Chris Rebert
On Tue, Sep 2, 2008 at 8:20 PM, Davy [EMAIL PROTECTED] 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

Re: Pass same parameter in Recursive function

2008-09-02 Thread Davy
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

Recursive function won't compile

2008-04-02 Thread bc1891
#include stdio.h #include stdlib.h def RecursiveFact(n): if(n1): 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 is this possible? --

Re: Recursive function won't compile

2008-04-02 Thread Gary Herron
[EMAIL PROTECTED] wrote: #include stdio.h #include stdlib.h def RecursiveFact(n): if(n1): 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 is

Re: Recursive function won't compile

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

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 stdio.h #include stdlib.h 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

Re: Recursive function won't compile

2008-04-02 Thread Diez B. Roggisch
[EMAIL PROTECTED] schrieb: #include stdio.h #include stdlib.h def RecursiveFact(n): if(n1): 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.

  1   2   >