Tuple assignment and generators?

Daniel Nogradi nogradi at gmail.com
Fri May 5 06:16:57 EDT 2006


> > I was just at a point when I thought I learned something but got
> > confused again after trying the following and unfortunately didn't
> > find an answer in the docs.
> >
> >>>> a = 10
> >>>> b = 10
> >>>> id(a)
> > 134536516
> >>>> id(b)
> > 134536516
> >
> > So the two memory addesses are the same, but
> >
> >>>> a = 10000
> >>>> b = 10000
> >>>> id(a)
> > 134604216
> >>>> id(b)
> > 134604252
> >
> > and they are not the same (I restarted the interpreter between the two
> > cases). So how is this now? Sorry if it's too trivial, but I simply
> > don't get it.
>
> It's an optimization scheme that will cache number objects up to a certain
> value for optimized reuse. However this is impractical for larger numbers -
> you only hold a table of lets say 1000 or so objects. Then the look up of
> one of those objects is extremely fast, whereas the construction of
> arbitrary numbers is somewhat more expensive.
>
> And as "is" is the operator for testing if objects are identical and _not_
> the operator for testing of equality (which is ==), the above can happen.
> And is totally irrelevant from a practical POV (coding-wise that is - it
> _is_ a relevant optimization).

Thanks a lot! So after all I really learned something :)



More information about the Python-list mailing list