Python Forum
Variables as labels vs fixed storage location
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variables as labels vs fixed storage location
#1
Is this a nit? For example.

>>> y = 5
>>> id(y)
4297636480
>>> y = 7
>>> id(y)
4297636544
>>> x = 5
>>> id(x)
4297636480
>>>

Clearly the "stuff" on the right-hand side are objects and have a fixed location. The y and x are just labels that point to the objects.

Most books say that variables are like boxes stored somewhere by the compiler/assembler/loader and the items that appear on the right-hand side get placed in the appropriate box.

Is this a "so what?" not a big enough deal to mention? Can not teaching that ever hurt anything?

Seems simpler to think of variables as boxes rather than labels that get hung on (point to) objects.

Not sure of the best way to handle this.

Thx,

C
Reply
#2
Well, you're also using small ints.  Those, in particular, will have the same id... but only on cpython.  Any other implementation of python (IronPython, jython, pypy, etc) might not work that way.  And that's only because of an optimization the cpython compiler uses (it assumes small ints are used fairly frequently, so it just caches them and shares them all over the place).

If you try the same thing with strings, it doesn't do that anymore...

# 50 is the same...
>>> id(50)
1426349088
>>> id(50)
1426349088
>>> id(50)
1426349088
>>> id(50)
1426349088

# but "50" is not the same...
>>> id("50")
31633984
>>> id("50")
31635328
>>> id("50")
31635296
>>> id("50")
31634464
>>> id("50")
31633920
Reply
#3
(Sep-21-2017, 07:08 PM)cnoblejr Wrote: Clearly the "stuff" on the right-hand side are objects and have a fixed location. The y and x are just labels that point to the objects.
id() show identity of an object the number is the object memory address.
x any y can both can point to same memory address.
>>> x = 5
>>> y = x
>>> id(x)
1434501200
>>> id(y)
1434501200
(Sep-21-2017, 07:08 PM)cnoblejr Wrote: Most books say that variables are like boxes stored somewhere by the compiler/assembler/loader and the items that appear on the right-hand side get placed in the appropriate box.
Python dos it an other way than most languages other-languages-have-variables.
Python has "names"
Quote:Seems simpler to think of variables as boxes rather than labels that get hung on (point to) objects.
No boxes is more correct to describe how other languages use variables.
This picture show the Python model,that name labels can get hung on same object in memory.
[Image: ab2tag.png]
Reply
#4
Interesting that the "50"s do not share the same location. Like the "labels hanging on the objects" image. Thx for the responses.

C
Reply
#5
"50" was maybe a bad example, but no two strings would share the same location, regardless of content.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 1,365 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Upload Files to Azure Storage Container phillyfa 6 2,980 Dec-22-2023, 06:11 AM
Last Post: Pedroski55
  Referencing a fixed cell Mark17 2 2,718 Dec-17-2020, 07:14 PM
Last Post: Mark17
  Secure App Data Storage for Kivy Android App JonPC 1 3,230 Nov-08-2019, 03:42 PM
Last Post: luke
  Real time graph of the temperatures and storage linkxxx 3 3,478 Aug-29-2019, 09:44 AM
Last Post: linkxxx
  Show real time temperature and storage with Python linkxxx 4 3,776 Aug-28-2019, 02:06 PM
Last Post: linkxxx
  show and storage real temperature data Arduino in Python3 linkxxx 0 2,258 Aug-21-2019, 04:07 PM
Last Post: linkxxx
  fixed width numbers Skaperen 15 12,346 May-27-2019, 09:42 AM
Last Post: Skaperen
  Need help with dezoomify code (fixed Q) Ezra 7 6,721 Nov-01-2017, 01:54 PM
Last Post: buran

Forum Jump:

User Panel Messages

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