By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,944 Members | 1,819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,944 IT Pros & Developers. It's quick & easy.

Local variables persist in functions?

P: n/a
I'm a bit baffled. Here is a bit of fairly straightforward code:

def _chunkify( l, chunkSize, _curList = list() ):
print _curList # yay for printf debugging
if len( l ) <= chunkSize:
_curList.append( l )
else:
newChunk = l[:chunkSize]
_curList.append( newChunk )
_chunkify( l[chunkSize:], chunkSize, _curList )
return _curList

_chunkify simply breaks a sequence into a sequence of smaller lists of
size <= chunkSize. The first call works fine, but if I call it
multiple times, weirdness happens.

chunks = _chunkify( list, size ) # _curList keeps its previous value!
chunks = _chunkify( list, size, list() ) # this works as expected

Considering the default value of _curList, these statements should be
identical. Any pointers? Did I miss something in the python reference
manual? (running 2.4.3, fyi)

Thanks,
Nils

Nov 24 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
12****@gmail.com wrote:
>
Considering the default value of _curList, these statements should be
identical. Any pointers? Did I miss something in the python reference
manual? (running 2.4.3, fyi)
See the FAQ:

http://www.python.org/doc/faq/genera...etween-objects
Nov 24 '06 #2

P: n/a

12****@gmail.com wrote:
I'm a bit baffled. Here is a bit of fairly straightforward code:

def _chunkify( l, chunkSize, _curList = list() ):
Quite apart from the default argument problem, which Duncan has
addressed, you have some problems with style and variable names. In
particular: give variables meaningful names ; "L".lower() is not
meaningful and also suffers from confusion with the digit 1 in some
fonts. There is no necessity for the _ in _curList in the above line.

Please consider reading http://www.python.org/dev/peps/pep-0008/
print _curList # yay for printf debugging
if len( l ) <= chunkSize:
_curList.append( l )
else:
newChunk = l[:chunkSize]
_curList.append( newChunk )
_chunkify( l[chunkSize:], chunkSize, _curList )
return _curList

_chunkify simply breaks a sequence into a sequence of smaller lists of
size <= chunkSize. The first call works fine, but if I call it
multiple times, weirdness happens.

chunks = _chunkify( list, size ) # _curList keeps its previous value!
chunks = _chunkify( list, size, list() ) # this works as expected
Is the first "list" a list, or is it the name of the same function that
you are calling to provide the 3rd argument?

[snip]

HTH,
John

Nov 24 '06 #3

P: n/a
12****@gmail.com wrote:
I'm a bit baffled. Here is a bit of fairly straightforward code:

def _chunkify( l, chunkSize, _curList = list() ):
print _curList # yay for printf debugging
if len( l ) <= chunkSize:
_curList.append( l )
else:
newChunk = l[:chunkSize]
_curList.append( newChunk )
_chunkify( l[chunkSize:], chunkSize, _curList )
return _curList

_chunkify simply breaks a sequence into a sequence of smaller lists of
size <= chunkSize. The first call works fine, but if I call it
multiple times, weirdness happens.

chunks = _chunkify( list, size ) # _curList keeps its previous value!
chunks = _chunkify( list, size, list() ) # this works as expected

Considering the default value of _curList, these statements should be
identical. Any pointers? Did I miss something in the python reference
manual? (running 2.4.3, fyi)
the default list() is only created once when the function is defined. And its later its always the same list
Use

def _chunkify( l, chunkSize, _curList=None ):
_curList = _curList or []
...

then it works.

Robert
Nov 24 '06 #4

P: n/a
<12****@gmail.comwrote:
chunks = _chunkify( list, size ) # _curList keeps its previous value!
chunks = _chunkify( list, size, list() ) # this works as expected

Considering the default value of _curList, these statements should be
identical. Any pointers?
http://effbot.org/pyfaq/why-are-defa...en-objects.htm
Did I miss something in the python reference manual? (running 2.4.3, fyi)
the paragraph that starts with "Default parameter values are evaluated when
the function definition is executed." in bold, perhaps. I've highlighted it
on this page:

http://effbot.org/pyref/def.htm

</F>

Nov 24 '06 #5

P: n/a
http://www.python.org/doc/faq/genera...etween-objects
Thanks for the link. I think I'll stick to None as the default value,
as that's a good way to keep the usability and make my code work ;)

Nov 24 '06 #6

P: n/a

John Machin wrote:
12****@gmail.com wrote:
I'm a bit baffled. Here is a bit of fairly straightforward code:

def _chunkify( l, chunkSize, _curList = list() ):

Quite apart from the default argument problem, which Duncan has
addressed, you have some problems with style and variable names. In
particular: give variables meaningful names ; "L".lower() is not
meaningful and also suffers from confusion with the digit 1 in some
fonts. There is no necessity for the _ in _curList in the above line.

Please consider reading http://www.python.org/dev/peps/pep-0008/
print _curList # yay for printf debugging
if len( l ) <= chunkSize:
_curList.append( l )
else:
newChunk = l[:chunkSize]
_curList.append( newChunk )
_chunkify( l[chunkSize:], chunkSize, _curList )
return _curList

_chunkify simply breaks a sequence into a sequence of smaller lists of
size <= chunkSize. The first call works fine, but if I call it
multiple times, weirdness happens.

chunks = _chunkify( list, size ) # _curList keeps its previous value!
chunks = _chunkify( list, size, list() ) # this works as expected

Is the first "list" a list, or is it the name of the same function that
you are calling to provide the 3rd argument?

[snip]

HTH,
John
Please consider reading http://www.python.org/dev/peps/pep-0008/
Done. Veru useful, thank you. Even though it's not the most correct
way or using the leading _, I was using it to sort of say 'don't set
this when calling', and yes--"L" is a bad name for a list, and I
probably should have used something else (even if this code is more of
a one-off than anything else).

Anyhow, thanks.

Nov 24 '06 #7

P: n/a
<12****@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
I'm a bit baffled. Here is a bit of fairly straightforward code:

def _chunkify( l, chunkSize, _curList = list() ):
print _curList # yay for printf debugging
Check out Winpdb at http://www.digitalpeers.com/pythondebugger/.

-- Paul
Nov 24 '06 #8

P: n/a
12****@gmail.com wrote:
I'm a bit baffled. Here is a bit of fairly straightforward code:

def _chunkify( l, chunkSize, _curList = list() ): ...
_chunkify simply breaks a sequence into a sequence of smaller lists of
size <= chunkSize. The first call works fine, but if I call it
multiple times, weirdness happens.

Considering the default value of _curList, these statements should be
identical. Any pointers? Did I miss something in the python reference
manual? (running 2.4.3, fyi)
You've already got the real answer. How about considering iterators,
since I presume you are chunking to help some kind of processing.:

def chunky(src, size):
'''Produce a (possibly long source) in size-chunks or less.'''
assert size 0
for start in range(0, len(src), size):
yield src[start : start + size]

def chunkify(alist, size, _curList=None):
if _curList is None:
return list(chunky(alist, size))
_curList.extend(list(chunky(alist, size)))
return _curList

I suspect most often you can use chunky directly:
for chunk in chunky(somedata, size):
...

for size in range(1, 30):
print size, list(chunky('abcdefghijklmnopqrstuvwxyz', size))
--Scott David Daniels
sc***********@acm.org
Nov 26 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.