python 中使用 if a == b == c: pass;` 是否有副作用?

发布于 2024-11-14 17:31:02 字数 358 浏览 0 评论 0原文

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

我们假设 a, b, c 是字符串变量。当且仅当所有三个字符串都相等时,如果我使用上面的代码片段执行 # do some ,是否会有任何可能的副作用?

我这样问是因为我必须相互检查三个变量,并且我遇到了很多情况:

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

也许有更好的方法来编码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软糖 2024-11-21 17:31:02

在您以这种方式使用之前,应该不会有任何副作用。

但请注意以下事项:

if (a == b) == c:

因为它会破坏链接,并且您将比较 TrueFalsec 值)。

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

默嘫て 2024-11-21 17:31:02

来自文档

比较可以任意链接;例如,x< y <= z 相当于 x <= y 和 y <= z,但 y 仅计算一次(但在这两种情况下,当发现 x < y 为 false 时,根本不会计算 z)。

应该没有副作用。

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.

謸气贵蔟 2024-11-21 17:31:02
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])
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])
白首有我共你 2024-11-21 17:31:02

对 x!=y!=z 有什么评论吗?

我可以用愚蠢的方法来得到正确的答案。

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 )

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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文