问题描述
在Python 2.7.5中:
In 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()
我得到:
为什么?
我所了解的关于threading.Event的一切,我从以下中学到的知识: http://docs.python.org/2/library/threading.html?highlight=threading#event-objects
Everything I know about threading.Event I learned from: http://docs.python.org/2/library/threading.html?highlight=threading#event-objects
当它说threading.Event()是类threading.Event的工厂函数时,这是什么意思? (呃……对我来说似乎只是普通的老样子).
What does it mean when it says that threading.Event() is a factory function for the class threading.Event ??? (Uhh... just looks like plain old instanciation to me).
推荐答案
threading.Event不是类,它是threading.py中的函数
threading.Event is not a class, it's function in 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子类化(尽管导入和使用带下划线的名称永远不是一个好主意):
Sinse this function returns _Event instance, you can subclass _Event (although it's never a good idea to import and use underscored names):
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()
这篇关于我如何子类threading.Event?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!