Hum*_*mad 1 python magic-methods
我在谷歌上搜索这些方法的任何用例或示例,但找不到任何详细解释,它们只是与其他类似方法一起列出。实际上,我在 github 上查看了一些代码并遇到了这些方法,但无法理解用法。有人可以提供这些方法的详细解释。这是我遇到它们的 github 代码链接:https : //github.com/msiemens/tinydb/blob/master/tinydb/queries.py
魔术方法__and__
,__or__
和__invert__
分别用于覆盖运算符a & b
,a | b
和~a
。也就是说,如果我们有一个类
class QueryImpl(object):
def __and__(self, other):
return ...
Run Code Online (Sandbox Code Playgroud)
然后
a = QueryImpl(...)
b = QueryImpl(...)
c = a & b
Run Code Online (Sandbox Code Playgroud)
相当于
a = QueryImpl(...)
b = QueryImpl(...)
c = a.__and__(b)
Run Code Online (Sandbox Code Playgroud)
这些方法被重写tinydb
以支持此语法:
>>> db.find(where('field1').exists() & where('field2') == 5)
>>> db.find(where('field1').exists() | where('field2') == 5)
# ^
Run Code Online (Sandbox Code Playgroud)
也可以看看:
归档时间: |
|
查看次数: |
2830 次 |
最近记录: |