Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python gevent-无法重载Greenlet子类的运算符_Python_Concurrency_Gevent_Greenlets - Fatal编程技术网

Python gevent-无法重载Greenlet子类的运算符

Python gevent-无法重载Greenlet子类的运算符,python,concurrency,gevent,greenlets,Python,Concurrency,Gevent,Greenlets,我在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()

我在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的源代码,但不明白为什么它不允许运算符重载。我觉得有一些元编程的黑魔法正在进行。有人有什么想法吗?

我很困惑,但它似乎是绿色的,奇怪的是a和b工作得很好。请注意,gevent.Greenlet子类Greenlet.Greenlet是一个C扩展模块,这使得调试变得有点困难。只要两个操作数都是同一类型的Echo或Actor,它似乎就可以工作。但是,当这两种类型不同时,它就失败了。。