[Grok-dev] meet the Grok 1.0 release team

Brandon Craig Rhodes brandon at rhodesmill.org
Wed Dec 3 07:23:56 EST 2008


"Martijn Faassen" <faassen at startifact.com> writes:

> I don't think the word 'pestering' is very appropriate.

I guess I was having fun. :-) I'll try to sound more official.

> To my knowledge tuples are allocated each time the code runs just as
> much as lists are, and not only when the program runs first, otherwise
> things like this wouldn't work:
>
> mytuple = a, b

It looks to me like the interpreter builds tuples of constants only
once:

>>> def foo():
...    return (1,2,3)
...
>>> a = foo()
>>> b = foo()
>>> id(a)
3084583396L
>>> id(b)
3084583396L

You're entirely correct, though, that the presence of variables makes
the tuple a dynamically-constructed variable rather than a constant; I
have never thought through the difference before.  But note that
constant sub-structures are only built once, even in the presence of
variables:

>>> def bar(x):
...    return (x,9,(1,2))
...
>>> a = bar(1.1)
>>> b = bar(1.2)
>>> id(a) # big tuple is different
3084584036L
>>> id(b)
3084583996L
>>> id(a[2]) # but little (1,2) tuples inside are the same
3084582220L
>>> id(b[2])
3084582220L

What fun.  Now I have a small topic for the Python Atlanta meetup next
week. :-)

-- 
Brandon Craig Rhodes   brandon at rhodesmill.org   http://rhodesmill.org/brandon


More information about the Grok-dev mailing list