[Python-Dev] == on object tests identity in 3.x

Chris Angelico rosuav at gmail.com
Tue Jul 8 04:15:27 CEST 2014


On Tue, Jul 8, 2014 at 11:59 AM, Rob Cliffe <rob.cliffe at btinternet.com> wrote:
> If I came across an int object and had no concept of what an integer number
> was, how would I know what its "value" is supposed to be?

The value of an integer is the number it represents. In CPython, it's
entirely possible to have multiple integer objects (ie objects with
unique identities) with the same value, although AIUI there are
Pythons for which that's not the case. The value of a float, Fraction,
Decimal, or complex is also the number it represents, so when you
compare 1==1.0, the answer is that they have the same value. They
can't possibly have the same identity (every object has a single
type), but they have the same value. But what *is* that value? It's
not something that can be independently recognized, because casting to
a different type might change the value:

>>> i = 2**53+1
>>> f = float(i)
>>> i == f
False
>>> f == int(f)
True

Ergo the comparison of a float to an int cannot be done by casting the
int to float, nor by casting the float to int; it has to be done by
comparing the abstract numbers represented. Those are the objects'
values.

But what's the value of a sentinel object?

_SENTINEL = object()
def f(x, y=_SENTINEL):
    do_something_with(x)
    if y is not _SENTINEL: do_something_with(y)

I'd say this is a reasonable argument for the default object value to
be identity.

ChrisA


More information about the Python-Dev mailing list