negative numbers are not equal...

Terry Reedy tjreedy at udel.edu
Fri Aug 15 15:00:39 EDT 2008



Mel wrote:
> castironpi wrote:
>> It would be nice to put together a really canonical case of the use of
>> the 'is' comparison.  FTSOA for the sake of argument, when do you use
>> it?  Why is it even in the language?
> 
> My poster child use case is in a MUDD game.  For instance, the player
> represented by `this_player` has picked up the yoghurt.  We notify the
> other players using code that boils down to:
> 
> for person in this_room.inhabitants:
>     if person is not this_player:
>         person.notify ('%s has picked up the %s.'
>                 % (this_player.name, 'yoghurt'))
> 
> The `is` test avoids telling this_player something he already knows. 
> Perhaps the code could be written to make an equality test work, but then
> again, perhaps the game could have a much more interesting use for equality
> between persons.

Excellent example.  There are three uses for 'is'.
1. Minor optimization of comparison with None, True, False.
2. Testing the implementation: 'a=1;b=1;a is b' *should* be True, while 
'a=257;b=257;a is b' *should* be False.  The CPython test suite has 
tests like this.
3. Comparision of user class objects where identify is important. 
Objects representing people is certainly such a case ;-).

tjr




More information about the Python-list mailing list