Explaining names vs variables in Python
Marko Rauhamaa
marko at pacujo.net
Wed Mar 2 09:11:55 EST 2016
Chris Angelico <rosuav at gmail.com>:
> On Thu, Mar 3, 2016 at 12:39 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> Chris Angelico <rosuav at gmail.com>:
>>
>>> Python defines that every object has an identity, which can be
>>> represented as an integer. Since this is an intrinsic part of the
>>> object, no two distinct objects can truly have identical
>>> characteristics. Python's objects are like rifles - there are many
>>> like it, but this one is mine.
>>
>> How can you be sure Python isn't returning the same id value for two
>> distinct objects?
>
> The same way I can be sure about anything else in Python. It's a
> language guarantee.
Actually, my question is (intentionally) nonsensical.
The sameness or distinctness of two objects is not directly defined in
Python. The definition is simply:
Two objects X and Y are called identical if
X is Y
evaluates to a true value.
Additionally, we have:
If objects X and Y are identical, it is guaranteed that
id(X) == id(Y)
evaluates to a true value.
Even more strongly, we have:
For any objects X and Y,
id(X) == id(Y) if X is Y else id(X) != id(Y)
evaluates to a true value.
What is missing is the rules that are obeyed by the "is" operator.
Marko
More information about the Python-list
mailing list