简体   繁体   English

python将元组作为函数参数传递

[英]python passing tuple as function parameter

I know this question sounds stupid, but I just cannot get it right. 我知道这个问题听起来很愚蠢,但我无法正确解决。

I want to start a new thread to execute this function with multiple arguments. 我想启动一个新线程以执行带有多个参数的此功能。 I don't know how to get it done. 我不知道该怎么做。 There are always error here. 这里总是有错误。

thread = threading.Thread(target=urllib2.urlopen, args=((url=REMOTE_SERVER_URL, data=context),))
thread.start()

The new thread would start this function. 新线程将启动此功能。

urllib2.urlopen(url=REMOTE_SERVER_URL, data=context)

Can anyone help me? 谁能帮我? how to get it right? 如何正确处理? Thanks 谢谢

Tuples don't take keyword arguments, they're not functions. 元组不接受关键字参数,它们不是函数。 If you want to pass in keyword arguments, pass a dictionary as the kwargs argument: 如果要传递关键字参数,则将字典作为kwargs参数传递:

thread = threading.Thread(target=urllib2.urlopen, kwargs={
    'url': REMOVE_SERVER_URL,
    'data': context
})

Or: 要么:

thread = threading.Thread(
    target=urllib2.urlopen,
    kwargs=dict(url=REMOVE_SERVER_URL, data=context)
)

Solution is already given above by @Blender. @Blender已经在上面给出了解决方案。

But little bit of explanation for this class Thread constructor . 但是对此类Thread constructor的解释很少。

In threading module, Class Thread doesn't use any *args and **kwargs, 在线程模块中,Class Thread不使用任何* args和** kwargs,

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

In the above snippet args is just an argument which default value is an empty tuple, and kwargs is another argument which default value is empty dictionary. 在上面的代码段中,args只是一个参数,其默认值为空元组,而kwargs是另一个参数,其默认值为空字典。 This is different from a function like def foo(*args, **kwargs) So, if you want to pass tuples argument please use args and if you want to pass dictionary argument please kwargs. 这与def foo(*args, **kwargs)类的函数不同,因此,如果要传递元组参数,请使用args ;如果要传递字典参数,请使用kwargs。

So, if you create an object like as below will run without any error, but which is wrong. 因此,如果您创建一个如下所示的对象,将运行而不会出现任何错误,但这是错误的。

>>> thread = threading.Thread(target=urllib2.urlopen, args='some argument which is not tuple', kwargs='some argument which is not dict')
>>> 

From the source code: please check the args and kwargs line, there is no unpacking there. 从源代码:请检查args和kwargs行,那里没有解包。

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

If you don't like this answer please, let me know in comment, I will remove it. 如果您不喜欢这个答案,请在评论中让我知道,我将其删除。

thanks 谢谢

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

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