Re: [Tutor] <var:data> assignment
Magnus Lycka
magnus at thinkware.se
Tue May 11 16:47:44 EDT 2004
Hi Denis, I think it's a really good initiative to bring something like
this up on the Tutor list. (This is obviously the right place to bring
up such questions.) I hope it will enlighten more people on the list.
To really understand these kinds of fundamental aspects of a programming
language, and of programming in general, is certainly a big advantage if
we want to be able to write working programs in a predictable manner.
You are very close to the Pythonic view of variables and names when you
draw the parallel with a dictionary. You are slightly off target though.
The id() function *does* tell you the location of the actual object in
question. There is no extra level of indirection involved.
Denis wrote:
> ---
> 'a' --> | 1 |
> ---
> ---
> 'b' --> | 1 | first model
> ---
> ---
> 'c' --> | 2 |
> ---
>
> or:
>
> ------
> 'a' --> | adr1 | \
> ------ \ ---
> > | 1 |
> ------ / ---
> 'b' --> | adr1 | / second model
> ------
>
> ------ ---
> 'c' --> | adr2 | --> | 2 |
> ------ ---
Both these models are wrong. What happens is that for some kinds of
immutable objects, that we are likely to use repeatedly, such as small
integers and short strings, Python doesn't create duplicates, but
reuses already created values/objects. This is called interning. In
other words, the correct model is as simple as this:
'a' \
\ ---
> | 1 |
/ ---
'b' /
---
'c' --> | 2 |
---
Note that it's only for particular object that this interned use
is utilized. E.g.
>>> i1 = 5
>>> id(i1)
8146912
>>> i2 = 5
>>> id(i2)
8146912
>>> i3 = 123456789
>>> id(i3)
8638132
>>> i4 = 123456789
>>> id(i4)
8566052
I.e.
'i1' \
\ ---
> | 5 |
/ ---
'i2' /
-----------
'i3' --> | 123456789 |
-----------
-----------
'i4' --> | 123456789 |
-----------
>>> s1 = "Hello"
>>> s2 = "Hello"
>>> s1 is s2
True
's1' \
\ -------
> | Hello |
/ -------
's2' /
>>> s1 = "Hello there, how is it out in the dark and cold world?"
>>> s2 = "Hello there, how is it out in the dark and cold world?"
>>> s1 is s2
False
--------------------------------------------------------
's1' --> | Hello there, how is it out in the dark and cold world? |
--------------------------------------------------------
--------------------------------------------------------
's2' --> | Hello there, how is it out in the dark and cold world? |
--------------------------------------------------------
If you know that you use a particular string often, or need to make it
faster to i.e. speed up dictionary access with this string as a key,
you can force Python to intern it. (It's only for strings you can do
this.)
>>> s3 = intern("Hello there, how is it out in the dark and cold world?")
>>> s4 = intern("Hello there, how is it out in the dark and cold world?")
>>> s3 is s4
True
's3' \
\ --------------------------------------------------------
> | Hello there, how is it out in the dark and cold world? |
/ --------------------------------------------------------
's4' /
I'm not sure exactly what algorithm Python uses to decide which objects to
intern automagically.
Python never interns mutable objects. Why?
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus at thinkware.se
More information about the Tutor
mailing list