"/a" is not "/a" ?

Carl Banks pavlovevidence at gmail.com
Mon Mar 9 11:47:56 EDT 2009


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.


Carl Banks



More information about the Python-list mailing list