newbie: confused with example in Learning Python 2nd Edition: can anyone give a hint

Discussion in 'Python' started by Porky Pig Jr, Aug 24, 2004.

  1. Porky Pig Jr

    Porky Pig Jr Guest

    Here is an example of Stack class which got me totally confused:

    >>> class Stack:

    .... def __init__(self, data):
    .... self._data = list(data)
    .... self.push = data.append
    .... self.pop = data.pop
    ....

    What I don't understand: we take the 'data' input (a list), and copy
    it to semi-private instance attribute _data. Something like this:

    >>> mystack = Stack([1,2,3])
    >>> dir(mystack)

    ['__doc__', '__init__', '__module__', '_data', 'pop', 'push']
    >>> mystack._data

    [1, 2, 3]

    So: why defitions of self.push and self.pop are defined as
    'data.append' rather than '_data.append', etc. What makes me yet more
    confused: the whole thing works just fine, and yet I can't figure out
    in which attribute we store the results of pushes and where pops are
    coming from.
    Like I push '4' on a stack:

    >>> mystack.push(4)


    and yet this does not affect _data (obviously):
    >>> mystack._data

    [1, 2, 3]

    and yet '4' is stored *somewhere*, since pop() produces the right
    result:

    >>> mystack.pop()

    4
    >>>


    Where is that hidden instance attribute and how can I access it? Seems
    like very simple definition, and yet there is something tricky about
    it.

    TIA.
     
    Porky Pig Jr, Aug 24, 2004
    #1
    1. Advertising

  2. Porky Pig Jr

    Duncan Smith Guest

    "Porky Pig Jr" <> wrote in message
    news:...
    > Here is an example of Stack class which got me totally confused:
    >
    > >>> class Stack:

    > ... def __init__(self, data):
    > ... self._data = list(data)
    > ... self.push = data.append
    > ... self.pop = data.pop
    > ...
    >
    > What I don't understand: we take the 'data' input (a list), and copy
    > it to semi-private instance attribute _data. Something like this:
    >
    > >>> mystack = Stack([1,2,3])
    > >>> dir(mystack)

    > ['__doc__', '__init__', '__module__', '_data', 'pop', 'push']
    > >>> mystack._data

    > [1, 2, 3]
    >
    > So: why defitions of self.push and self.pop are defined as
    > 'data.append' rather than '_data.append', etc. What makes me yet more
    > confused: the whole thing works just fine, and yet I can't figure out
    > in which attribute we store the results of pushes and where pops are
    > coming from.
    > Like I push '4' on a stack:
    >
    > >>> mystack.push(4)

    >
    > and yet this does not affect _data (obviously):
    > >>> mystack._data

    > [1, 2, 3]
    >
    > and yet '4' is stored *somewhere*, since pop() produces the right
    > result:
    >
    > >>> mystack.pop()

    > 4
    > >>>

    >
    > Where is that hidden instance attribute and how can I access it? Seems
    > like very simple definition, and yet there is something tricky about
    > it.
    >
    > TIA.


    >>> alist = [1,2,3]
    >>> mystack = Stack(alist)
    >>> mystack.push(4)
    >>> mystack._data

    [1, 2, 3]
    >>> alist

    [1, 2, 3, 4]

    Duncan
     
    Duncan Smith, Aug 24, 2004
    #2
    1. Advertising

  3. Porky Pig Jr wrote:

    > Here is an example of Stack class which got me totally confused:
    >
    >>>> class Stack:

    > ... def __init__(self, data):
    > ... self._data = list(data)
    > ... self.push = data.append
    > ... self.pop = data.pop


    It should have confused you. It was wrong.

    From the errata at
    http://www.oreilly.com/catalog/lpython2/errata/lpython2.confirmed:

    {457} class Stack: code;
    self.push and self.pop should both reference self._data, not just data

    So the corrected code should look like:
    >>> class Stack:

    ... def __init__(self, data):
    ... self._data = list(data)
    ... self.push = _data.append
    ... self.pop = _data.pop

    And now the class should act like you expect. Before it was modifying the
    list (data) it was passed in the constructor. Now it modifies its own copy
    (_data).

    By the way, a brief note on errata. IMO it is counter-productive to dwell
    on errata (although a quick skim doesn't hurt). Trying to notice each
    error distracts from understanding. Usually the brain glosses over the
    error and reads what was intended rather than what was said.
     
    Steven Rumbalski, Aug 24, 2004
    #3
  4. Porky Pig Jr

    Dave Cole Guest

    Re: newbie: confused with example in Learning Python 2nd Edition:can anyone give a hint

    Steven Rumbalski wrote:
    > Porky Pig Jr wrote:
    > So the corrected code should look like:
    >
    >>>>class Stack:

    >
    > ... def __init__(self, data):
    > ... self._data = list(data)
    > ... self.push = _data.append
    > ... self.pop = _data.pop


    >>>class Stack:

    .... def __init__(self, data):
    .... self._data = list(data)
    .... self.push = self._data.append
    .... self.pop = self._data.pop

    --
    http://www.object-craft.com.au
     
    Dave Cole, Aug 24, 2004
    #4
    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. Daniel Pope
    Replies:
    5
    Views:
    481
    Silvio Bierman
    Feb 22, 2004
  2. Ron Stephens

    Learning Python, 2nd Edition

    Ron Stephens, Jan 4, 2004, in forum: Python
    Replies:
    0
    Views:
    279
    Ron Stephens
    Jan 4, 2004
  3. Robert Brewer
    Replies:
    4
    Views:
    400
    Porky Pig Jr
    Aug 24, 2004
  4. Garg
    Replies:
    4
    Views:
    966
  5. chhenning
    Replies:
    5
    Views:
    139
    Dave Thomas
    Jan 17, 2007
Loading...

Share This Page