Local variables persist in functions?

120psi at gmail.com 120psi at gmail.com
Fri Nov 24 10:36:56 EST 2006


John Machin wrote:
> 120psi at 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.




More information about the Python-list mailing list