Local variables persist in functions?

Discussion in 'Python' started by 120psi@gmail.com, Nov 24, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Duncan Booth Guest

    Duncan Booth, Nov 24, 2006
    #2
    1. Advertising

  3. John Machin Guest

    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
     
    John Machin, Nov 24, 2006
    #3
  4. robert Guest

    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
     
    robert, Nov 24, 2006
    #4
  5. <> wrote:

    > 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-default-values-shared-between-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>
     
    Fredrik Lundh, Nov 24, 2006
    #5
  6. Guest

    , Nov 24, 2006
    #6
  7. Guest

    John Machin wrote:
    > 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, 2006
    #7
  8. Paul McGuire Guest

    <> wrote in message
    news:...
    > 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
     
    Paul McGuire, Nov 24, 2006
    #8
  9. 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
     
    Scott David Daniels, Nov 26, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dew Baboeram

    session variables don't persist

    Dew Baboeram, Aug 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,192
    Kevin Spencer
    Aug 20, 2004
  2. Raed Sawalha

    session variables don't persist ,why?

    Raed Sawalha, Sep 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,226
    Hermit Dave
    Sep 30, 2004
  3. =?Utf-8?B?dGhleWFzQGNvbW11bml0eS5ub3NwYW0=?=

    aspnet_wp does not persist (local debuggging)

    =?Utf-8?B?dGhleWFzQGNvbW11bml0eS5ub3NwYW0=?=, Dec 16, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    496
    =?Utf-8?B?dGhleWFzQGNvbW11bml0eS5ub3NwYW0=?=
    Jan 3, 2005
  4. Sullivan WxPyQtKinter
    Replies:
    10
    Views:
    707
    Antoon Pardon
    Nov 8, 2007
  5. Dave
    Replies:
    2
    Views:
    573
    Chris Rebert
    May 15, 2010
Loading...

Share This Page