"/a" is not "/a" ?

Steve Holden steve at holdenweb.com
Sat Mar 14 10:20:32 EDT 2009


Carl Banks wrote:
> On Mar 8, 5:32 am, Lie Ryan <lie.1... at gmail.com> wrote:
>> Mel wrote:
>>>  wrote:
>>>> Steven D'Aprano <st... at pearwood.info> writes:
>>>>> It is never
>>>>> correct to avoid using "is" when you need to compare for identity.
>>>> When is it ever necessary to compare for identity?
>>> Ho-hum.  MUDD game.
>>> def broadcast (sender, message):
>>>     for p in all_players:
>>>         if p is not sender:
>>>             p.tell (message)    # don't send a message to oneself
>> Since in a MUD game, a player would always have a unique username, I'd
>> rather compare with that. It doesn't rely on some internals. There is
>> very, very rare case where 'is' is really, really needed.
> 
> Well, by that criterion you can dismiss almost anything.
> 
> Of course you can assign unique ids to most objects and perform your
> identity tests that way.  The point is that sometimes you do need to
> test for the identity of the object, not merely the equivalent
> semantic value.
> 
> If, faced with this problem (and I'm guessing you haven't faced it
> much) your approach is always to define a unique id, so that you can
> avoid ever having to use the "is" operator, be my guest.  As for me, I
> do program in the sort of areas where identity testing is common, and
> I don't care to define ids just to test for identity alone, so for me
> "is" is useful.
> 
Well, the obvious "identity" is id(p), but then

  a is b

is entirely equivalent to

  id(a) == id(b)

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
Holden Web LLC                 http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/




More information about the Python-list mailing list