Interesting behaviour of the assignment
Moshe Zadka
moshez at zadka.site.co.il
Sun Dec 31 10:31:43 EST 2000
[Thomas Wouters]
> In other words, 'x == y' first does 'x is y', and if it's true, the
> result
> of the operation is true. Only if 'x is y' is false will it start the
> (possibly very expensive) comparison-by-contents operation.
[Michael Esveldt]
> This is a somewhat ignorant question, but if 'x == y' checks for 'x is
> y' first, why use 'is' at all? If you only get a tiny bit of speed out
> of using 'is' is it worth it to have this in the language at all? (Not
> that it would ever change, just curious.)
[Alex Martelli]
> Moshe gave an example: *defeating* polymorphism attempts on
> the part of object implementers, based on their implementing
> __cmp__. Why one would actively work to impede polymorphism,
> I don't know, but it sure can work that way.
Here's an example where I'd rather use "is" then "==":
# module foo.py
SPAM = 0
EGGS = 1
def eat(what):
if what is SPAM:
eat_spam()
if what is EGGS:
eat_eggs()
raise FoodError()
# user code:
import foo
foo.eat(foo.SPAM)
foo.eat(foo.EGGS)
The semantics are accurate here: foo.eat should not be passed 0.0,
it should be passed only foo.SPAM or foo.EGGS, so a FoodError
will alert the programmer to his bug sooner.
--
Moshe Zadka <sig at zadka.site.co.il>
This is a signature anti-virus.
Please stop the spread of signature viruses!
More information about the Python-list
mailing list