我遇到了gevent和greenlet的一个特殊问题,我无法排除故障。为了方便起见,我试图在gevent.Greenlet的子类中重载运算符:
import gevent
class Actor(gevent.Greenlet):
# other stuff..
def __or__(self, other):
print "Hello from or!"
class Echo(Actor):
pass
a = Actor()
b = Echo()
# This works!
print a.__or__(Echo())
# This doesn't!!
print a | b
这是输出:
$ python gtest.py
Hello from or!
None
Traceback (most recent call last):
File "gtest.py", line 20, in <module>
print a | b
TypeError: unsupported operand type(s) for |: 'Actor' and 'Echo'
我查看了gevent.Greenlet
的源代码,但无法理解为什么它会禁止运算符重载。我觉得有一些元编程黑魔法正在进行中。有没有人有想法?