negative numbers are not equal...
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Fri Aug 15 14:27:16 EDT 2008
Terry Reedy a écrit :
>
>
> 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
make it four.
> uses for 'is'.
> 1. Minor optimization of comparison with None, True, False.
Warning (to any python newbie reading this): x is True is *very*
different from x == True. IOW ;: don't use 'is' with True and False
unless you know *exactly* what you're doing.
> 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 ;-).
4. Anywhere you want to test identity.
More information about the Python-list
mailing list