在 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()
I get:
TypeError:调用元类基时出错
function() 参数 1 必须是 code,而不是 str
Why?
我所知道的关于线程的一切。我从中学到的事件:http://docs.python.org/2/library/threading.html?highlight=threading#event-objects 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)