[Tutor] Why isn't my simple if elif script not working?

Steven D'Aprano steve at pearwood.info
Wed Jul 18 16:44:50 CEST 2012


Alexandre Zani wrote:
> On Wed, Jul 18, 2012 at 6:09 AM, Steven D'Aprano <steve at pearwood.info> wrote:
>> Alexandre Zani wrote:
>>
>>> What you want to write is this:
>>>
>>> elif name == "John Cleese" or name == "Michael Palin":
>>
>> elif name in ("John Cleese", "Michael Palin"):
>>
>> is better.

> Better how?


It's shorter, you don't have to repeat the reference to `name` twice, it is 
more easily extensible if you add additional names, and it is likely to be a 
*tiny* bit faster -- it moves the equality test out of pure-Python code into a 
tuple method, which will be written in C.



-- 
Steven


More information about the Tutor mailing list