开发者

Are there sideeffects in python using `if a == b == c: pass;`?

if a == b == c:
    # do something

Let's assume a, b, c are string variables. Are there any possible side effects if I use the snippet above to execute # do something if and only if all three strings are equal?

开发者_开发技巧

I am asking because I have to check three variables against each other and I get many cases:

if a == b == c:
    # do something
elif a == b != c:
    # do something
elif a != b == c.
    # do something
etc...

Perhaps there is a better way to code this?


There should be no side effects until you use it in a such way.

But take care about things like:

if (a == b) == c:

since it will break chaining and you will be comparing True or False and c value).


From the documentation:

Comparisons can be chained arbitrarily; for example, x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

There should be no side effects.


s = set([a, b, c])

if len(s) == 1:
    print 'All equal'
elif len(s) == 3:
    print 'All different'
else:
    l = list(s)
    print '%s and %s are different' % (l[0], l[1])


is there any comment on x!=y!=z ?

i could use the stupid way to get a correct answer.

def aligndigits(list):

   return ((x, y , z ) for x in list for y in list for z in list if   x != y and y != z and x != z )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜