473,408 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Local variables persist in functions?

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
8 1259
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

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

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

Similar topics

8
by: pertheli | last post by:
I am in a situation where only "goto" seems to be the answer for my program logic where I have to retry calling some repeated functions. Can anybody help in the usage of goto and its effect in...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
6
by: The Colonel | last post by:
In a .vb class function, can I assign user-specific information (e.g. First Name, Role, etc.) to local variables (as opposed to referring to the session variable multiple times)?
1
by: Wolfgang | last post by:
Hi all, I've started to write some functions but I have some problems with common variables in that functions. So I have some variables which should be accessible by all my functions but not...
21
by: Sriram Rajagopalan | last post by:
Hi, Which of these two would be a better way of initializing the local variables? and why? 1) At the time of their declaration. Eg: void func()
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
by: John Davison | last post by:
When you have the following: function foo() { var bar = 10; // do some other stuff } It is my understanding that bar is *not* a property of function object foo. Now, when you have the...
8
by: Samant.Trupti | last post by:
Hi All, I am facing a strange problem.. I am calling a function func2 from func1. Before calling func2 all the local variables in func1 looks fine and has their respective values. When...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.