[Tutor] basic Qs [tutorial / text editors / performance characteristics]

Gonçalo Rodrigues op73418@mail.telepac.pt
Fri Feb 14 08:06:01 2003


----- Original Message -----
From: "siddharth karandikar" <siddharth178@hotmail.com>
To: <tutor@python.org>
Sent: Friday, February 14, 2003 12:50 PM
Subject: Re: [Tutor] basic Qs [tutorial / text editors / performance
characteristics]


>
> doing following with lists ->
>
> >>>a = [1, 2, 3, 4, 5]
>
> >>>b = a                 # <--- what does this actually means
>

b and a are different *names* for the same list. Think of a and b as
references to objects and you won't be far off. Notice though, that Python
dereferences automatically => there are no pointers in Python.

> >>>b.append(6)
>
> >>>a
> [1, 2, 3, 4, 5, 6]        # <-- is a and b are references of
>                             the same list object ?
>

Since b and a reference the same list b.append(6) or a.append(6) are
effectively the same.

>
>
> how to create new objects from other object ???
> something like new instance with data of other object.  ( is object is
right
> word to use )
>
>

Ok you want a *copy* of a, right? A different object but with equal
contents. There are various ways. Here goes the simplest:

b = list(a)

Here goes another:

b = []
for elem in a:
    b.append(elem)

Hope it helps,
G. Rodrigues