[Tutor] Discussion about Chaining in comparison operators

Alan Gauld alan.gauld at yahoo.co.uk
Tue Oct 6 20:45:28 EDT 2020


On 07/10/2020 01:09, Joel Goldstick wrote:

>> Our objective should *always* be to write the most readable(*)
>> code we can.
>>
>> So yes, a==b==c wins.

> another way is:
> 
>>>> a=3
>>>> b=4
>>>> c=3
>>>> len(set((a,b,c)))

> if they are all the same the set will be of length 1.


That's true but it is neither readable nor performant.
It requires an act of thought to recognize that 3 items
into a set of length 1 means they are all the same.
That's much less readable than a direct equality test.
And much slower since you have to construct a set.

a==b==c

still wins

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list