when to use == and when to use is

Ben Finney ben+python at benfinney.id.au
Mon Mar 10 22:16:49 CET 2014


George Trojan <george.trojan at noaa.gov> writes:

> Both if statements work, of course. Which is more efficient?

I don't know. The answer is likely to be dependent on many details of
the code and the data.

But I do know that the different operators communicate different
intents.

And that should be a primary reason for choosing which operator to use:
communicate your intent to the reader of the code.

> My use-case scenario are matplotlib objects, the __eq__ operator might
> involve a bit of work.

Then IMO you should choose the operator which matches your intent, and
stop worrying about “efficient” until you have measured exactly which
part of the code is slow.

It sounds like you are interested in value equality. So you should use
‘==’ for this purpose.

-- 
 \        “A free press is one where it's okay to state the conclusion |
  `\                      you're led to by the evidence.” —Bill Moyers |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list