[BangPypers] python id function

Praveen Kumar praveen.python.plone at gmail.com
Thu Jan 7 14:05:40 CET 2010


An immutable object is an object whose state cannot be modified after it is
created. This is in contrast to a mutable object, which can be modified
after it is created
As you said
>>If I try to create new immutable object, It is just returning the existed
object instead of creating new
( *that is not exactly to create new immutable object* if you want to create
new immutable object A new object has to be created if a different value has
to be stored) so in your case x=tuple() and y=tuple() has the same value.
Objects whose value can change are said to be mutable; objects whose value
is unchangeable once they are created are called immutable
An object can be either entirely immutable or some attributes in the object
may be declared immutable; for example, using the const member data
attribute in the C++ programming language. In some cases, an object is
considered immutable even if some internally used attributes change but the
object's state appears to be unchanging from an external point of view

I tested some of stuff ( i divided in left hand side[contains same id] and
right hand side[different id] )

*>>> id(10)
14046508
>>> a=10  // assigning the same value
>>> id(a)
14046508*
In Python everything is an object so integer is an object. The limits now
are set by the amount of memory you have in your computer. If you want to
store 5,000 digits long, go ahead. Typing it or reading it will be the only
problem! How does Python do all of this? It automatically manages the
integer object, which is initially set to 32 bits for speed. If it exceeds
32 bits, then Python increases its size as needed up to the RAM limit
>>> l=[1,2,3]
                        >>> id(l)
                            14645248
                       >>> m=[1,2,3]
                       >>> id(m)
                            14656216
                        >>> msg="hi"
                        >>> id(msg)
                            14647680
                        >>> he="hello"
                        >>> id(he)
                            14648032
                        >>> again="again"
                        >>> id(again)
                            14647968
                        >>> t=(1,2,3)
                        >>> id(t)
                            14646208
                        >>> m=(1,2,3)
                        >>> id(m)
                            14645408


                        >>> c=[];
                        >>> d=[];
                        >>> id(c)
                            14646088
                        >>> id(d)
                            14646128
*>>> c=d=[]
>>> id(c)
    14656176
>>> id(d)
    14656176
msg="hi"
>>> id(msg)
14647712
>>> id(msg.strip('i'))
14647680
>>> id(msg)
14647712*
The method strip() will not change the data "hi" thats contains. Instead, a
new String object is instantiated and given the data "h" during its
construction. A reference to this String object is returned by the strip()
method. To make the String msg contain the data "h", a different approach is
needed.

Thanks

On Thu, Jan 7, 2010 at 2:45 PM, Anand Balachandran Pillai <
abpillai at gmail.com> wrote:

> On Thu, Jan 7, 2010 at 2:16 PM, Anand Balachandran Pillai <
> abpillai at gmail.com> wrote:
>
> >
> >
> >
> >
> > 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+.
> >
>
> I wrote a small program to find this out. And
> on my Python runtime (2.6.2), the answer is 257.
>
> def intCacheLimit():
>    i,l,m=1,range(500),range(500)
>
>    while i<500:
>        x=l[i]
>        y=m[i]
>        if x is not y:
>            print 'Limit',x
>            break
>
>        i+=1
>
> [anand at localhost python]$ python
> Python 2.6.2 (r262:71600, Aug 21 2009, 12:23:57)
> [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from intlimit import *
> Limit 257
>
> So Python caches integers till 256 - a nice square integer.
> Cool right ?
>
> Others can try it out in other Python versions - my guess
> would be it is the same everywhere.
>
>
> >
> >
> >
>
>
> --
> --Anand
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
Praveen Kumar
+91 9620621342
http://praveensunsetpoint.wordpress.com
Bangalore


More information about the BangPypers mailing list