[BangPypers] python id function
Anand Balachandran Pillai
abpillai at gmail.com
Thu Jan 7 09:46:08 CET 2010
On Thu, Jan 7, 2010 at 1:44 PM, leela vadlamudi
<leela.vadlamudi at gmail.com>wrote:
> Hi,
>
> Python docs says that id function returns the unique id for an object.
>
> >>> id(10)
> 165936452
> >>> a=10
> >>> id(a)
> 165936452
> >>> b = int(10)
> >>> id(b)
> 165936452
>
> >>> x = tuple()
> >>> y=tuple()
> >>> id(x)
> -1208311764
> >>> id(y)
> -1208311764
>
> >>> l = list()
> >>> m = list()
> >>> id(l)
> -1210839956
> >>> id(m)
> -1210839700
>
> >From the above example, id(mutable_object) returns different ids, but
> id(immutable_object) return always the same id. If I try to create new
> immutable object, It is just returning the existed object instead of
> creating new. How does it internally manages to return the same object?
> Why
> it is not creating new object if it is immutable?
>
Python caches immutable objects - which is why they are immutable
and saves memory if u reference the same immutable in many
places. So small integers, empty tuples and such are actually
cached in Python runtime, so you keep getting back the same object
and hence the same id.
A few examples (as above).
>>> id(())
140641378807888
>>> id(())
140641378807888
>>> id(x)
140641378807888
>>> id(1)
15611384
>>> x=1
>>> id(x)
15611384
>>> y=1
>>> id(y)
15611384
>>> x=99
>>> id(x)
15613016
>>> y=99
>>> id(y)
15613016
>>> x=100
>>> id(x)
15612992
But this is only valid till a limit. For egs,
>>> x=500
>>> y=500
>>> id(x)
16843192
>>> id(y)
16843240
It is a good exercise to find out the limit till Python caches
integers. My guess is that it is somewhere close to 100, i.e
100+.
Lists are mutable, hence you get different objects when you
create different lists with same contents.
>
> What about this below case?
>
> >>> id((1,))
> -1208770004
> >>> id((1,))
> -1208770004
> >>> a=(1,)
> >>> id(a)
> -1208745460
> >>> id((1,))
> -1208759028
>
> Why is id changes here even if it is a tuple(immutable).
>
>>> id((1,))
16398480
>>> a=(1,)
>>> id(a)
16398480
So far so good...
But,
>>> id((1,))
16398608
The id of ((1,)) changes after it is bound to a variable. Not sure
why this happens.
However this doesn't mean that Python caches *all* tuples in
memory. As I said caching is done only for integers upto a limit
and for "trivial" immutables like the empty () etc. Not for
*all* immutables.
For egs.
>>> x=(1,5,7)
>>> y=(1,5,7)
>>> x==y
True
>>> x is y
False
>>> id(x)
16384960
>>> id(y)
16384400
Strings are much better cached in Python than tuples.
>>> x='python'
>>> y='python'
>>> id(x)
140641378993328
>>> id(y)
140641378993328
If you create two strings containing same value one after
another, they are cached most of the time...
>>> y='12345678910987654321'
>>> x='12345678910987654321'
>>> id(x)
16398640
>>> id(y)
16398640
--Anand
_______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
--
--Anand
More information about the BangPypers
mailing list