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

P

Porky Pig Jr

Here is an example of Stack class which got me totally confused:
.... 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:

and yet this does not affect _data (obviously):[1, 2, 3]

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

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

Duncan Smith

Porky Pig Jr said:
Here is an example of Stack class which got me totally confused:
... 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:

and yet this does not affect _data (obviously):[1, 2, 3]

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

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
 
S

Steven Rumbalski

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

... 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: ... 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.
 
D

Dave Cole

Steven said:
Porky Pig Jr wrote:
So the corrected code should look like:


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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top