是不是bool __invert__行为“奇怪”? [英] Isn't bool __invert__ behaviour "strange"?
问题描述
为什么要从int中继承bool __invert__或__neg__还没有被b / b
覆盖以产生布尔否定?我怀疑向后
兼容性或类似的东西,但我仍然想知道..
因为bool不能被子类化,所以有类似bool的类型但是用
布尔否定你有什么建议?包装器可能是?
(我会使用它,所以我可以评估用户定义的布尔表达式
在我的命名空间中创建他/她的变量的istances而不是eval()
他/她的表达)
Saizan写道:
为什么要从int中继承bool __invert__或__neg__没有?
被覆盖以产生布尔否定?
我想知道-True或-False应该评估什么。
问候,
Bj?rn
-
BOFH借口#297:
太多中断
Bjoern Schliessmann写道:
Saizan写道:
< blockquote class =post_quotes>
为什么要从int中继承bool __invert__或__neg__没有
被覆盖以产生布尔否定?
我不知道-True或-False应该评估什么。
问候,
Bj?
-
BOFH借口#297:
太多中断
布尔符号--True == False和-False == True,实际上你可能更喜欢?b $ b?或者术语上的一行,但由于python中没有这样的
运算符,我认为我们应该使用 - 这也是Bool自己使用的运算符
用于否定的公式,即1-x。
现在我认为1-x应该作为否定在Python中,
,因为True == 1和False == 0,但以这种方式写表达式会有点烦人。
Saizan写道:
为什么要从int中继承bool __invert__或__neg__还没有
覆盖产生布尔否定?我怀疑向后
兼容性或类似的东西,但我仍然想知道..
因为bool不能被子类化,所以有类似bool的类型但是用
布尔否定你有什么建议?一个包装器可能?
(我会使用它,所以我可以评估用户定义的布尔表达式
在我的命名空间中创建他/她的变量的istances而不是eval()
他/她的表达式)
not运算符和bool()内置产生
布尔结果。由于bool是int的子类,
所有整数运算都将保持整数
运算。这是为了后退
兼容性,并且不太可能在2.x
系列中改变。
我不喜欢不记得这是否应该在3.0中改变
。参见PEP 3100和3099.
John Roth
John Roth
Why subclassing bool from int either __invert__ or __neg__ haven''t been
overrided to produce a boolean negation? I suspect backwards
compatibility or something alike, but I still wonder..
And since bool can''t be subclassed, to have a type like bool but with
boolean negation what do you suggest? A wrapper maybe?
(I would use it so I can evaluate user-defined boolean expression
creating istances of his/her variables in my namespace and than eval()
his/her expression)
Saizan wrote:
Why subclassing bool from int either __invert__ or __neg__ haven''t
been overrided to produce a boolean negation?I wonder what -True or -False should evaluate to.
Regards,
Bj?rn
--
BOFH excuse #297:
Too many interrupts
Bjoern Schliessmann wrote:Saizan wrote:
Why subclassing bool from int either __invert__ or __neg__ haven''t
been overrided to produce a boolean negation?
I wonder what -True or -False should evaluate to.
Regards,
Bj?rn
--
BOFH excuse #297:
Too many interruptsWell in boolean notation -True == False and -False == True, actually
you may prefer ? or a line over the term, but since there''s no such
operator in python I think we should use "-" which is also the operator
used by Bool himself in his formulation for negation which was 1-x.
Now that I think of it 1-x should work as a negation in Python, too,
since True == 1 and False == 0, but it would be a little annoying to
write expressions in this way.
Saizan wrote:Why subclassing bool from int either __invert__ or __neg__ haven''t been
overrided to produce a boolean negation? I suspect backwards
compatibility or something alike, but I still wonder..
And since bool can''t be subclassed, to have a type like bool but with
boolean negation what do you suggest? A wrapper maybe?
(I would use it so I can evaluate user-defined boolean expression
creating istances of his/her variables in my namespace and than eval()
his/her expression)The not operator and the bool() builtin produce
boolean results. Since bool is a subclass of int,
all the integer operations will remain integer
operations. This was done for backwards
compatability, and is unlikely to change in the 2.x
series.
I don''t remember if this is supposed to change
in 3.0. See PEP 3100 and 3099.
John Roth
John Roth
这篇关于是不是bool __invert__行为“奇怪”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!