Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Solution 2:

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).

Solution 3:

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

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

Solution 4:

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

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

defaligndigits(list):

   return ((x, y , z ) for x inlistfor y inlistfor z inlistif   x != y and y != z and x != z )

Post a Comment for "Are There Sideeffects In Python Using `if A == B == C: Pass;`?"