threading.Thread.__init__
期望(最多)group
, target
, name
, args
,kwargs
和verbose
关键字参数。
由于您有大量额外的参数(可能超过threading.Thread.__init__
预期的六个),那么显式提取这六个并处理其余的可能会更少
self.__dict__.update(**kwargs)
import threading
class Test(threading.Thread):
def __init__(self, **kwargs):
super(Test, self).__init__(
**{k: v for k in 'group target name args kwargs verbose'.split()
if k in kwargs})
self.__dict__.update(**kwargs)
A = None
B = 1
test = Test(A=A, B=B)
print test.A
print test.B
请注意,如果您__init__
不带参数调用:
super(Test, self).__init__()
threading.Thread
那么将不会设置by 使用的一大堆属性:
class Thread(_Verbose):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):
assert group is None, "group argument must be None for now"
_Verbose.__init__(self, verbose)
if kwargs is None:
kwargs = {}
self.__target = target
self.__name = str(name or _newname())
self.__args = args
self.__kwargs = kwargs
self.__daemonic = self._set_daemon()
self.__ident = None
self.__started = Event()
self.__stopped = False
self.__block = Condition(Lock())
self.__initialized = True
# sys.stderr is not stored in the class like
# sys.exc_info since it can be changed between instances
self.__stderr = _sys.stderr
我不认为这是你想要发生的...