Python Forum
Question about primitive variables.
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about primitive variables.
#17
There is only one guarantee that Python enforces for id(): the id() of an object will not change as long as that object exists. Nothing more, nothing less. Every other behavior you see is more or less an accident.  Smile

Here are some examples.

>>> a=100;b=100;id(a) == id(b)
True
>>> c=100;id(a) == id(c)
True
In this example, a, b, and c are all references to the same cached value of 100 that Python creates during startup. This is simple, straight-forward, and creates much confusion.


>>> a=1000;b=1000;id(a) == id(b)
True
>>> c=1000;id(a) == id(c)
False
What's happening here? Shouldn't both inputs return False. Nope. In the first input, the parser detects that the integer 1000 is created twice and decides to have both a and b refer to the same copy of 1000. This is legal since the parser knows that the instance of 1000 does not get deleted while the parser is running so it can safely refer to it twice. In the second input, the parser has no knowledge of the previous creation of 1000, so a new instance is created.

So does that mean the parser has to know the integer values that are cached? Nope. The parser extracts the string representing the number and calls an internal function PyLong_FromString() that returns a reference to an object that represents the numeric value of the string. For integers in the range -5 to 256, PyLong_FromString() will return references to the cached objects.

Okay, so now I understand. Numeric values between -5 and 256 will always point to the same internal object. Nope. Look at the following example:

>>> a=1
>>> b=int('3'*33)
>>> c=(b * b + 1) % b
>>> a == c; id(a) == id(c)
True
False
a is a reference to the cached internal value of 1. c is the result of a calculation and is a reference to a new instance of the value 1.

Does that mean all calculations result in new instances of the result? Nope. See the next example.

>>> a=1
>>> b=int('3'*3)
>>> c=(b * b + 1) % b
>>> a == c; id(a) == id(c)
True
True
In the first calculation, the value of b*b+1 is large enough to require the use of the multiple precision routines included in Python. The multiple precision routines create a new instance of Python integer object to store the remainder. Once the result is calculated, it is just returned. No check is performed to see if the result is in the range -5 to 256. In the second calculation, the value of b*b+1 is small enough to fit in a C "long" type and the calculation is done using the C "long" type. The result is converted to a Python integer using the internal function PyInt_FromLong(). PyInt_FromLong() does check if the result can refer to a cached object.

The behavior of id() and is is confusing. id() is best used as a debugging tool. is should only be used to check if an object is exactly equal to None, True, or False. Those three objects are defined (in Python 3) to be "singleton" objects: only a single instance of those objects can exist.

And here is one more confusing example where two identical objects do not compare as equal.

>>> a=float('nan')
>>> b=a
>>> a is b; a == b; (a,) == (b,)
True
False
True
nan is special floating point value that indicates the value is "not a number". It is usually generated as the result of an error. The IEEE-754/854 standards for floating point arithmetic require that a nan must compare as not equal to every other value, including itself. That rule generates the False for the second line of the output. But as a performance optimization when comparing lists and tuples, Python assumes that the same object will always be equal to itself. That optimization generates the True on the third line of output.

I hope this helps and doesn't cause too much confusion,

casevh
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 205 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Question regarding local and global variables donmerch 12 5,130 Apr-12-2020, 03:58 PM
Last Post: TomToad
  Question about naming variables in class methods sShadowSerpent 1 2,024 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Basic Pyhton for Rhino 6 question about variables SaeedSH 1 2,157 Jan-28-2020, 04:33 AM
Last Post: Larz60+
  A question about global variables Goldberg291 3 4,017 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