[Tutor] Discussion about Chaining in comparison operators

Joel Goldstick joel.goldstick at gmail.com
Tue Oct 6 20:09:52 EDT 2020


On Tue, Oct 6, 2020 at 7:58 PM Alan Gauld via Tutor <tutor at python.org> wrote:
>
> On 06/10/2020 18:32, Manprit Singh wrote:
>
> > now if i have to check that if all three are equal or not, what should i
> > prefer from the below given choices :
> >
> > 1) a == b  and a == c
> > or it chained version
> > b == a == c
> >
> > 2)   a == b and b == c
> > or its chained version
> >  a == b == c
> >
> > Since a == b == c seems more  readable than  b == a == c, I will prefer
> > this .
>
> Our objective should *always* be to write the most readable(*)
> code we can.
>
> So yes, a==b==c wins.
>
> (*) The reason for this is that in any serious code, maintenance
> accounts for 80% of the total project cost. Therefore anything
> which improves the maintainability of the code is a big win
>  - and readability makes code much more maintainable.
>
> For example, if a bit of code increases coding time by 10% but
> reduces maintenance costs by 10% the net effect is +2% in
> development but -8% in maintenance = an overall 6% reduction
> in cost! Software engineering, like any other kind of engineering,
> is all about building solutions at lowest cost.
> The rare exception to this rule is where performance is critical
> and maintainability may need to be compromised, but that hardly ever
> happens in the real world because there are usually better solutions
> to fixing performance.
>
> --
> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

another way is:

>>> a=3
>>> b=4
>>> c=3
>>> len(set((a,b,c)))
2
>>> b = 3
>>> len(set((a,b,c)))
1
>>>

if they are all the same the set will be of length 1.
-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays


More information about the Tutor mailing list