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

Paul Rubin http
Mon Aug 23 21:57:59 EDT 2004


"Robert Brewer" <fumanchu at amor.org> writes:
> > So: why defitions of self.push and self.pop are defined as
> > 'data.append' rather than '_data.append', etc.

It's an error.  It should say _data.append.

> Hint: Python doesn't have variables like other languages do. The line:
> 
>     self._data = list(data)
> 
> does not make a copy of "data". Instead, it binds a new name
> (self._data) to the same object which the name "data" refers to.

The list function does make a copy.

    Python 2.3.4 (#1, Jul 24 2004, 19:45:58)
    [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> j=[9,8]
    >>> a=list(j)
    >>> a is j
    False
    >>> j.append(2)
    >>> j
    [9, 8, 2]
    >>> a
    [9, 8]
    >>>



More information about the Python-list mailing list