在 Python 2.7.5 中:
from threading import Event
class State(Event):
def __init__(self, name):
super(Event, self).__init__()
self.name = name
def __repr__(self):
return self.name + ' / ' + self.is_set()
我得到:
TypeError: Error when calling the metaclass bases
function() argument 1 must be code, not str
为什么?
我所知道的关于线程的一切。我从中学到的事件:http://docs.python.org/2/library/threading.html?highlight=threading#event-objects
threading.Event() 是类 threading.Event 的工厂函数是什么意思??? (呃......对我来说就像普通的旧实例)。
最佳答案
threading.Event 不是一个类,它是 threading.py 中的函数
def Event(*args, **kwargs):
"""A factory function that returns a new event.
Events manage a flag that can be set to true with the set() method and reset
to false with the clear() method. The wait() method blocks until the flag is
true.
"""
return _Event(*args, **kwargs)
由于此函数返回 _Event 实例,您可以将 _Event 子类化(尽管导入和使用带下划线的名称绝不是一个好主意):
from threading import _Event
class State(_Event):
def __init__(self, name):
super(Event, self).__init__()
self.name = name
def __repr__(self):
return self.name + ' / ' + self.is_set()
关于python - 我如何子类化 threading.Event?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17998651/