同时分配 [英] simultaneous assignment

查看:60
本文介绍了同时分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将多个变量分配给相同的值,但是那么

身份测试仍然评估为False?


例如:< br>

w = x = y = z = False
w
False x
假w == x
真w是x



真#不确定这是否有害


上面的第一行是我知道的唯一方法,但似乎

必然导致真正的身份测试。

解决方案

John Salerno写道:

有没有办法将多个变量分配给相同的值,但是身份测试仍然可以评估为假?

例如:

>>> w = x = y = z = False
>>> w False>>> x False>>> w == x True>>> w是x真#不确定这是否有害

上面的第一行是我知道的唯一方法,但它似乎必然会导致真正的身份测试。




是的,没有办法解决这个问题 - 假设python没有一个

可重载赋值运算符,例如C ++提供(赞美BDFL!)。在python中看起来像赋值的

语义是我在某个地方有一个花哨的

对象,现在我希望能够通过它来引用它那很好

这里的名字。实际上,对于

列表/字典元素分配,情况稍微复杂一些,但对于你的情况则无关紧要。


此外,取决于你分配的内容(例如整数,以及我猜的布尔值

身份测试结果如何。考虑一下:

a = 1000000
b = 1000000
a是b
假a = 100
b = 100 a是b
假a = 10
b = 10
a是b
True



我无法想象你在这之后的实际情况,但假设你确实需要这个并且我们正在谈论真实的话。这里的对象,序列

拆包和分配可能会派上用场,还有一个小的

list-comp:


a,b ,c = [copy.copy(o)for i in xrange(3)]

Diez


John Salerno写道:

有没有办法将多个变量分配给相同的值,但是身份测试仍然评估为False?

例如:

>>> w = x = y = z = False
>>> w False>>> x False>>> w == x True>>> w是x真#不确定这是否有害

上面的第一行是我知道的唯一方法,但它似乎必然会导致真正的身份测试。


class Foo:
def __eq __(* blah):返回False


w = x = y = z = Foo()
w == x
假w是x
True



Boris Borcic写道:

>>> w == x False>>> w是x True>>>




这与我想要发生的情况相反。


Is there a way to assign multiple variables to the same value, but so
that an identity test still evaluates to False?

e.g.:

w = x = y = z = False
w False x False w == x True w is x


True # not sure if this is harmful

The first line above is the only way I know to do it, but it seems to
necessarily lead to the true identity test.

解决方案

John Salerno wrote:

Is there a way to assign multiple variables to the same value, but so
that an identity test still evaluates to False?

e.g.:

>>> w = x = y = z = False
>>> w False >>> x False >>> w == x True >>> w is x True # not sure if this is harmful

The first line above is the only way I know to do it, but it seems to
necessarily lead to the true identity test.



Yes, and there is no way around that - given that python has not an
overloadable assignment operator as e.g. C++ offers (praise the BDFL!). The
semantics of what looks like assignment in python is "I''ve got a fancy
object somewhere, and now I want to be able to refer to it by that nice
name here". Actually things are somewhat more complicated in case of
list/dictionary element assignment, but for your case that is irrelevant.

Besides, depending on what you assign (e.g. integers, as well as the boolean
constants I guess) you might not even have the choice to guarantee how the
identity test results anyway. Consider this:

a = 1000000
b = 1000000
a is b False a = 100
b = 100
a is b False a = 10
b = 10
a is b True



I can''t imagine what you''re actually after here, but assuming that you
really need this and that we''re talking "real" objects here, sequence
unpacking and assignment might come handy here, together with a small
list-comp:

a, b, c = [copy.copy(o) for i in xrange(3)]
Diez


John Salerno wrote:

Is there a way to assign multiple variables to the same value, but so
that an identity test still evaluates to False?

e.g.:

>>> w = x = y = z = False
>>> w False >>> x False >>> w == x True >>> w is x True # not sure if this is harmful

The first line above is the only way I know to do it, but it seems to
necessarily lead to the true identity test.


class Foo : def __eq__(*blah) : return False

w = x = y = z = Foo()
w == x False w is x True



Boris Borcic wrote:

>>> w == x False >>> w is x True >>>



That''s the opposite of what I want to happen.


这篇关于同时分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆