Addressing the last element of a list

Matt Hammond matt.hammond at rd.bbc.co.uk
Tue Nov 8 05:55:50 EST 2005


On Tue, 08 Nov 2005 10:25:21 -0000, Jerzy Karczmarczuk  
<karczma at info.unicaen.fr> wrote:

> Would you please concentrate on - what I underlined - the sense of "C"  
> aliasing,
> where you can make a pointer to point to anything, say, the 176th byte  
> of a
> function code?

Pretty much everything is referred to by reference in python. The big  
difference is that most
primitive data types (integer, string) etc... aren't mutable - you can't  
change them in place - instead you replace them with a new instance  
containing the new/modified value.

But your own object and lists are mutable - you can change their  
attributes. So encapsulate data within a list or as an attribute of an  
object and you've got the effect you're after.

For example:

>>> a = 5; b = a
>>> a = 6
>>> a,b
(6, 5)

Whereas:

>>> a = [5]; b = a
>>> a[0] = 6
>>> a,b
([6], [6])

Note that reassigning a:
>>> a = [6]
causes a to point to a new list, containing the value 6, whereas the 2nd  
example above modified the list by replacing an element of it.


Hope this helps


Matt

-- 

| Matt Hammond
| R&D Engineer, BBC Research & Development, Tadworth, Surrey, UK.
| http://kamaelia.sf.net/
| http://www.bbc.co.uk/rd/



More information about the Python-list mailing list