On 07/30/2018 07:20 AM, Sunil Tech wrote:
> Hi Team,
> 
> I am investigating how the memory allocation happens in Python

There are several things going on here, but the main thing to know is:
but Python language _implementations_ optimize when they can and think
it makes sense. So don't draw too many conclusions from this
experiment... The python.org Python only creates one copy of small
integer objects, I think I've read somewhere the range is -5 to 256.
Strings get some kind of the same treatement - short strings may get
entries in the bytecode generated (.pyc files) allowing references to be
to the same id (id does not necessarily mean memory location, by the way)

Pretty much, don't count on this behavior.  Other Pythons may not do the
same things.

There is some control over whether strings are interned, see sys.intern()



> For Eg:
> *Case 1:*
> 
>>>> a = 10
>>>> b = 10
>>>> c = 10
>>>> id(a), id(b), id(c)
> (140621897573616, 140621897573616, 140621897573616)
>>>> a += 1
>>>> id(a)
> 140621897573592
> 
> 
> *Case 2:*
> 
>>>> x = 500
>>>> y = 500
>>>> id(x)
> 4338740848
>>>> id(y)
> 4338741040
> 
> 
> *Case 3:*
> 
>>>> s1 = 'hello'
>>>> s2 = 'hello'
>>>> id(s1), id(s2)
> (4454725888, 4454725888)
>>>> s1 == s2
> True
>>>> s1 is s2
> True
>>>> s3 = 'hello, world!'
>>>> s4 = 'hello, world!'
>>>> id(s3), id(s4)
> (4454721608, 4454721664)
>>>> s3 == s4
> True
>>>> s3 is s4
> False
> 
> Python memory allocation is varying in all these use cases. Please help me
> understand.
> 
> Thanks,
> Sunil. G
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to