1

I ran into a peculiar issue with gevent and greenlet that I can't troubleshoot. I'm trying to overload an operator for convenience in a subclass of 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

This is the output:

$ 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'

I looked at the source code for gevent.Greenlet but couldn't see why it would disallow operator overloading. I feel like there is some meta-programming black magic going on. Does anyone have an idea?

4

0 回答 0