编辑布尔值和运算符
Edit boolean and operator
所以我一直在摆弄 classes 中的标准运算符来尝试看看我能做什么,但我一直没能找到如何编辑布尔值 and
运算符。
我可以通过定义 __and__(self)
来编辑按位 &
运算符,但不能编辑 and
的行为方式。有谁知道我如何改变 a and b
的行为,其中 a
和 b
是我正在制作的 class 的实例?
提前致谢!
and
运算符使用 __bool__
将第一个操作数转换为布尔值,然后对布尔值执行预定义操作(如果 first.__bool__()
为 True
,return 第二,否则 return 第一)。无法更改此行为。
在Python2中,and
和or
访问__nonzero__
:
>>> class Test(object):
... def __nonzero__(self):
... print '__nonzero__ called'
... return True
...
>>> Test() and 1
__nonzero__ called
1
在Python3中,__nonzero__
已重命名为__bool__
。
>>> class Test:
... def __bool__(self):
... print('__bool__ called')
... return True
...
>>> Test() and 1
__bool__ called
1
请注意,短路评估可能会抑制对 __nonzero__
或 __bool__
的调用。
>>> 0 and Test()
0
>>> 1 or Test()
1
另一个需要注意的特性是 Python 试图访问 __len__
如果 __nonzero__
/ __bool__
没有定义并且如果__len__
return 是 0
以外的值。如果两种方法都定义了,__nonzero__
/ __bool__
获胜。
>>> class Test:
... def __len__(self):
... return 23
...
>>> Test() and True
True
>>>
>>> class Test:
... def __len__(self):
... return 23
... def __bool__(self):
... return False
...
>>> Test() and True
<__main__.Test object at 0x7fc18b5e26d8> # evaluation stops at Test() because the object is falsy
>>> bool(Test())
False
Is there any way i can have this return something other than a bool, like, say, a list of bools?
不幸的是,没有。 documentation 指出方法 应该 return False
或 True
但实际上如果你得到 TypeError
让它 return 别的东西。
>>> class Test:
... def __bool__(self):
... return 1
...
>>> Test() and 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __bool__ should return bool, returned int
>>>
>>> bool(Test())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __bool__ should return bool, returned int
所以我一直在摆弄 classes 中的标准运算符来尝试看看我能做什么,但我一直没能找到如何编辑布尔值 and
运算符。
我可以通过定义 __and__(self)
来编辑按位 &
运算符,但不能编辑 and
的行为方式。有谁知道我如何改变 a and b
的行为,其中 a
和 b
是我正在制作的 class 的实例?
提前致谢!
and
运算符使用 __bool__
将第一个操作数转换为布尔值,然后对布尔值执行预定义操作(如果 first.__bool__()
为 True
,return 第二,否则 return 第一)。无法更改此行为。
在Python2中,and
和or
访问__nonzero__
:
>>> class Test(object):
... def __nonzero__(self):
... print '__nonzero__ called'
... return True
...
>>> Test() and 1
__nonzero__ called
1
在Python3中,__nonzero__
已重命名为__bool__
。
>>> class Test:
... def __bool__(self):
... print('__bool__ called')
... return True
...
>>> Test() and 1
__bool__ called
1
请注意,短路评估可能会抑制对 __nonzero__
或 __bool__
的调用。
>>> 0 and Test()
0
>>> 1 or Test()
1
另一个需要注意的特性是 Python 试图访问 __len__
如果 __nonzero__
/ __bool__
没有定义并且如果__len__
return 是 0
以外的值。如果两种方法都定义了,__nonzero__
/ __bool__
获胜。
>>> class Test:
... def __len__(self):
... return 23
...
>>> Test() and True
True
>>>
>>> class Test:
... def __len__(self):
... return 23
... def __bool__(self):
... return False
...
>>> Test() and True
<__main__.Test object at 0x7fc18b5e26d8> # evaluation stops at Test() because the object is falsy
>>> bool(Test())
False
Is there any way i can have this return something other than a bool, like, say, a list of bools?
不幸的是,没有。 documentation 指出方法 应该 return False
或 True
但实际上如果你得到 TypeError
让它 return 别的东西。
>>> class Test:
... def __bool__(self):
... return 1
...
>>> Test() and 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __bool__ should return bool, returned int
>>>
>>> bool(Test())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __bool__ should return bool, returned int