简体   繁体   English

将带有** kwargs错误的值线程化并将其传递给TypeError

[英]Threading and passing values with **kwargs errors to TypeError

I'm pretty new to Python and was looking into using threading for some code via this post: Python - Using threads or a queue to iterate over a for loop that calls a function 我对Python很陌生,并且正在通过这篇文章探讨使用线程处理一些代码: Python-使用线程或队列来迭代调用函数的for循环

I was wondering why this simple example code errors out to 我想知道为什么这个简单的示例代码错误

Error: line 1: TypeError: file <maya console> line 4: __init__() got
an unexpected keyword argument 'A' #

My code: 我的代码:

import threading
class Test(threading.Thread):
    def __init__(self, **kwargs):
        super(Test, self).__init__( **kwargs)
        self.__dict__.update(**kwargs)

A = None
B = 1   
test = Test(A = A, B = B)
print test.A
print test.B

My assumption is it has to do with super(Test, self).__init__( **kwargs) call, but I'm not sure how to work around it. 我的假设是它与super(Test, self).__init__( **kwargs)调用有关,但是我不确定如何解决它。 My goal is pass in a rather large amount of arguments which is why I'm using **kwargs to begin with. 我的目标是传递大量参数,这就是为什么我要使用**kwargs原因。

You're passing the arguments A and B to the Thread constructor, which doesn't need them. 您将参数A和B传递给Thread构造函数,不需要它们。 Probably you should just call the super constructor with no arguments. 可能您应该只调用不带参数的超级构造函数。

threading.Thread.__init__ expects (at most) group , target , name , args , kwargs and verbose keyword arguments. threading.Thread.__init__期望(最多) grouptargetnameargskwargsverbose关键字参数。

Since you have a large number of extra arguments (presumably more than the six that threading.Thread.__init__ expects), then it may be less work to explicity extract those six and handle the rest with 由于您有大量额外的参数(大概超过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

Note, if you call __init__ with no arguments: 注意,如果不带任何参数调用__init__

super(Test, self).__init__()

then a whole bunch of attributes used by threading.Thread will not be set: 那么将不会设置threading.Thread使用的所有属性:

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

I don't think that is what you want to happen... 我不认为那是你想发生的事情...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM