Python:TypeError:* 之后的参数必须是一个序列 [英] Python: TypeError: argument after * must be a sequence

查看:116
本文介绍了Python:TypeError:* 之后的参数必须是一个序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,我尝试在新线程中发送 UDP 数据报

I have this piece of code in which I try to send an UDP datagram in a new thread

import threading, socket

address = ("localhost", 9999)


def send(sock):
    sock.sendto("Message", address)
    print "sent"

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
threading.Thread(target=send, args=(s)).start()

但是当我尝试将套接字作为函数的参数时,会抛出 TypeError 异常:

But when I try to give the socket as an argument to the function, a TypeError exception is thrown:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in 
    self.__target(*self.__args, **self.__kwargs)
TypeError: send() argument after * must be a sequence, not _socketobject

这是什么意思?

推荐答案

您需要在变量 s 后添加逗号 - ,.仅将 s 发送到 args=() 试图解压缩多个参数,而不是仅发送单个参数.

You need to add a comma - , - after your variable s. Sending just s to args=() is trying to unpack a number of arguments instead of sending just that single arguement.

所以你有 threading.Thread(target=send, args=(s,)).start()

还有 splat - * - 运算符可能在 this 解释其用法和一般解压参数的问题

Also the splat - * - operator might be useful in this question explaining it's usage and unzipping arguments in general

这篇关于Python:TypeError:* 之后的参数必须是一个序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆