T
Tobiah
print float(3.0) is float(3.0)
True
Thanks,
Tobiah
True
Thanks,
Tobiah
False
Tobiah said:Subject: Seemingly odd 'is' comparison.
Your values are already all floats so float() just returns its arguments.Thanks,
Tobiah
Thumb rule: Never compare strings, numbers or tuples with "is". Only
compare an object with a singleton like a type or None. "is" is not a
comparison operator.
Except for documented singletons such as modules and None, which objects
have the same identity is platform dependent, version dependent, and even
dependent on the execution history of your code.
But given the nature of immutables
Except for documented singletons such as modules and None, which objects
have the same identity is platform dependent, version dependent, and even
dependent on the execution history of your code.
news:[email protected]...
| The advice not to identity test strings and numbers (since they are
| interred in the main implementation),
They may or may not be.
Ditto for tuples, unless possibly when they have mutable members.
| But given the nature of
| immutables, is the identity of these even potentially implementation
| dependant (ie. they couldn't be interred could they)?
The word is 'interned', not 'interred' (buried).
Arnaud said:False
[You don't need to wrap your floats in float()]
... return 3.0 is 3.0, 3.0*1.0 is 3.0
...2 0 LOAD_CONST 1 (3.0)
3 LOAD_CONST 1 (3.0)
6 COMPARE_OP 8 (is)
9 LOAD_CONST 3 (3.0)
12 LOAD_CONST 1 (3.0)
15 COMPARE_OP 8 (is)
18 BUILD_TUPLE 2
21 RETURN_VALUE
As you can see when "3.0 is 3.0" is evaluated the same float object is
put on the stack twice so the 'is' comparison is True (LOAD_CONST 1 /
LOAD_CONST 1 / COMPARE_OP 8).
Whereas when "3.0*1.0 is 3.0" is evaluated, *two* different float
objects are put on the stack and compared (LOAD_CONST 3 / LOAD_CONST
1 / COMPARE_OP 8). Therefore the result is False.
Boris Borcic said:Looks good, but doesn't pass the sanity checkConsider
return 3 is 3, 3*1 is 3
2 0 LOAD_CONST 1 (3)
3 LOAD_CONST 1 (3)
6 COMPARE_OP 8 (is)
9 LOAD_CONST 3 (3)
12 LOAD_CONST 1 (3)
15 COMPARE_OP 8 (is)
18 BUILD_TUPLE 2
21 RETURN_VALUE
(True, True)
Duncan said:s/different/possibly different depending on implementation details/
Arnaud's point remains valid: in the first comparison you can see that the
same object is used, in the second case all bets are off.
Steven said:"is" is a comparison operator: it compares identity, not equality. It is
more or less equivalent to the expression id(x) == id(y).
3084440752135689200
with
Asun said:So was that a yes or no? I mean is it even possible for the identity
behaviour of mutables to vary between implementations? I can't see
how they can possibly be interned, but is there some other factor I'm
missing in regard to identity behaviour which could in fact vary
between implementations?
Christian Heimes said:3084440752
[You don't need to wrap your floats in float()]... return 3.0 is 3.0, 3.0*1.0 is 3.0def f():
...2 0 LOAD_CONST 1 (3.0)f() (True, False)
import dis
dis.dis(f)
3 LOAD_CONST 1 (3.0)
6 COMPARE_OP 8 (is)
9 LOAD_CONST 3 (3.0)
12 LOAD_CONST 1 (3.0)
15 COMPARE_OP 8 (is)
18 BUILD_TUPLE 2
21 RETURN_VALUEAs you can see when "3.0 is 3.0" is evaluated the same float object is
put on the stack twice so the 'is' comparison is True (LOAD_CONST 1 /
LOAD_CONST 1 / COMPARE_OP 8).Whereas when "3.0*1.0 is 3.0" is evaluated, *two* different float
objects are put on the stack and compared (LOAD_CONST 3 / LOAD_CONST
1 / COMPARE_OP 8). Therefore the result is False.
Looks good, but doesn't pass the sanity checkConsider
>>> def f():
return 3 is 3, 3*1 is 3
>>> import dis
>>> dis.dis(f)
2 0 LOAD_CONST 1 (3)
3 LOAD_CONST 1 (3)
6 COMPARE_OP 8 (is)
9 LOAD_CONST 3 (3)
12 LOAD_CONST 1 (3)
15 COMPARE_OP 8 (is)
18 BUILD_TUPLE 2
21 RETURN_VALUE
>>> f()
(True, True)
Cheers, BB
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.