git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

True is True / False is False?


I know for ints, cpython caches something like -127 to 255 where `is`
works by happenstance based on the implementation but not the spec
(so I don't use `is` for comparison there because it's not
guaranteed by the language spec). On the other hand, I know that None
is a single object that can (and often *should*) be compared using
`is`. However I spent some time reading through the language specs and
didn't encounter anything about booleans returned from
comparisons-operators, guaranteeing that they always return The One
True and The One False.

  x = 3 == 3 # some boolean expression evaluating to True/False
  y = 4 > 0 # another boolean expression
  if x is y:
    print("cool, same as always")
  else:
    print("is this ever possible?")

Is there some theoretical world in which that `else` branch could ever
be hit because the True referenced by x is not the same True
referenced by y? (assuming non-pathological overriding of dunder
methods to return true-ish or false-ish values; or at least assuming
any dunder-overriding is pure standard-library)

In the big picture, this is likely irrelevant and I should just use
"==" instead, but I got the question stuck in my head and ended up
bothered that I couldn't disinter an answer from docs.

Thanks,

-tkc