newbie Q: sequence membership

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Nov 19 14:55:45 EST 2007


En Mon, 19 Nov 2007 03:32:12 -0300, saccade <trivik at gmail.com> escribió:

> So if I am permitted to think of integers as immutable objects with
> predefined labels (i.e. the integers used in the text of the program
> code) that cannot de or re referenced then what a similar treatment of
> characters will look like seams to be an arbitary (a design) decition.
>
> In this vein it seams reasonable to expect 'a'[0] and 'ba'[1] to refer
> to the same object. If one follows the convention used with integers
> (9 and 9 refer to the same object) then 'ab' and 'ab' would be the
> same. An equally reasonable assumption would be that 'ab' and 'ab' are
> two different sequences and so not equal (I do not see the problem
> here).

Note that the fact that integers are immutable means that Python *could*  
share the same integer object any time that integer appears on the  
program. For mutable objects --lists by example-- this is not possible  
because different instances of the "same" list may be changed  
independently.
In the following example, both x and y *could* refer to the same object,  
but they don't:

py> x=1000
py> y=1000
py> x is y
False

In contrast, small integers are shared:

py> x=10
py> y=10
py> x is y
True

So immutability is a *necessary* condition for literals to be shared (in  
the Python object model), but it's not *sufficient*.

-- 
Gabriel Genellina




More information about the Python-list mailing list