Python Forum
Question about primitive variables.
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about primitive variables.
#11
(Apr-01-2017, 11:00 PM)snippsat Wrote:
(Apr-01-2017, 10:27 PM)Nirelg Wrote: They are both int after all, so why when I write twice 2**2 it gives me the same id yet when I do the same for 2**11 it gives them different ids?
Integers between -5 and 256 are cached in Python.
This optimization strategy makes sense because small integers pop up all over the place,
and given that each integer takes 24 bytes, it saves a lot of memory for a typical program.

So id() show memory address.
>>> help(id)
Help on built-in function id in module builtins:

id(...)
    id(object) -> integer
    
    Return the identity of an object.  This is guaranteed to be unique among
    simultaneously existing objects.  (Hint: it's the object's memory address.)

>>> id(10)
491765920
>>> id(10)
491765920
>>> id(10)
491765920
>>> id(100)
491767360
>>> id(100)
491767360
>>> id(100)
491767360
>>> # Now over 256
>>> id(1000)
58284576
>>> id(1000)
51896640

To be honest, when I am doing the same thing you do, I am getting different results...
when I use "**" my program works as it should:
print id(2**8)
print id(2**8)
print id(2**9)
print id(2**9)
Output:
44072652 44072652 44819164 44819152
Yet when i write it like this:
print id(256)
print id(256)
print id(512)
print id(512)
Output:
45252300 45252300 45998848 45998848
Even the ints above 256 have the same id.
I tried it both with larger numbers and from cmd with the same result...
any idea why it works like this?
Reply


Messages In This Thread
Question about primitive variables. - by Nirelg - Apr-01-2017, 05:06 PM
RE: Question about primitive variables. - by Nirelg - Apr-01-2017, 08:25 PM
RE: Question about primitive variables. - by wavic - Apr-01-2017, 09:04 PM
RE: Question about primitive variables. - by Nirelg - Apr-01-2017, 10:27 PM
RE: Question about primitive variables. - by wavic - Apr-01-2017, 09:52 PM
RE: Question about primitive variables. - by Nirelg - Apr-02-2017, 10:11 PM
RE: Question about primitive variables. - by Ofnuts - Apr-02-2017, 08:37 PM
RE: Question about primitive variables. - by wavic - Apr-03-2017, 12:13 AM
RE: Question about primitive variables. - by Nirelg - Apr-03-2017, 01:01 AM
RE: Question about primitive variables. - by casevh - Apr-03-2017, 02:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 216 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Question regarding local and global variables donmerch 12 5,144 Apr-12-2020, 03:58 PM
Last Post: TomToad
  Question about naming variables in class methods sShadowSerpent 1 2,026 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Basic Pyhton for Rhino 6 question about variables SaeedSH 1 2,159 Jan-28-2020, 04:33 AM
Last Post: Larz60+
  A question about global variables Goldberg291 3 4,022 Feb-02-2017, 10:50 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020