is list always with reference ?

holger krekel pyth at devel.trillke.net
Tue Aug 27 09:09:09 EDT 2002


Nikola Plejic wrote:
> Actually, not all of them...
> 
> If you do this:
> 
> >>> list1 = ["my", "sample", "list"]
> >>> list2 = list1
> >>> list2
> ['my', 'sample', 'list']
> >>> list2 = list1 * 2

If you keep in mind that 'list1' and 'list2' are 

    names which are bound to an object 

things get easier to understand.  And note, that 
the '=' is used to express the (re)binding operation.

'list1*2'        returns a new list object
'list2 = ...'    binds the righthandside object to the name 'list2'

issuing a 

>>> list1.append("!")

is *not* a rebinding operation.  it modifies the list object
*in-place* and thus has no affect on any name bindings.

hope this de-construction helps,

    holger




More information about the Python-list mailing list