Python Forum
Shared reference of variables...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shared reference of variables...
#1
What I understand is Python uses shared reference for variables.

I verified by assigning two variables with value of 10 (int). They both had the same memory address.

>>> a = 10
>>> b = 10
>>> hex(id(a))
'0x7fe4741d69c0'
>>> hex(id(b))
'0x7fe4741d69c0'
But when I assigned a value of 500, they have difference memory address.
>>> c = 500
>>> d = 500
>>> hex(id(c))
'0x7fe4669f6a30'
>>> hex(id(d))
'0x7fe4669f6b30'
How come? What changed in value 500?

Thanks.
Reply
#2
Python caches small integers,which are integers between -5 and 256.
These numbers are used so frequently that it's better for performance to already have these objects available.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick UnboundLocalErrors: variables reference before assignment? Need Help! sirLancelot 1 1,568 Mar-08-2021, 04:29 PM
Last Post: deanhystad
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,559 Sep-07-2020, 08:02 AM
Last Post: perfringo
  How to install and use a shared libary, via a .dll? ninjaisfast 0 1,299 Jul-09-2020, 03:23 PM
Last Post: ninjaisfast
  Divisors shared the second numbers mircea_dragu 1 2,046 Feb-07-2019, 10:09 PM
Last Post: ichabod801
  I can't use file __init__ to store shared variables and classes in the package AlekseyPython 2 3,339 Feb-04-2019, 06:26 AM
Last Post: AlekseyPython
  running just one process shared among uses Skaperen 3 2,990 Aug-07-2018, 12:12 AM
Last Post: Skaperen
  Shared reference and equality zyo 3 3,162 Jun-30-2018, 07:10 PM
Last Post: ljmetzger
  Shared queues l00p1n6 3 2,992 May-15-2018, 01:38 PM
Last Post: DeaD_EyE
  two different objects, but somehow the values are shared between them RaphaelMG 6 4,129 Apr-20-2018, 05:53 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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