[Tutor] Python Memory Allocation -- deep learning
Joel Goldstick
joel.goldstick at gmail.com
Mon Jul 30 11:09:55 EDT 2018
On Mon, Jul 30, 2018 at 9:20 AM, Sunil Tech <sunil.techspk at gmail.com> wrote:
> Hi Team,
>
> I am investigating how the memory allocation happens in Python
> 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 at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
An id is not guaranteed to be a memory address. In the cases you
cited, your version of python caches small integers, and apparently
small strings because its convenient and efficient to do so. Other
implementations of python may take a different approach. I think it
would be unwise to ever assume that a value is cached in your day to
day programming
--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
More information about the Tutor
mailing list