Python: TypeError: argument after * must be a sequence

你。 提交于 2021-02-06 10:47:10

问题


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()

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

What that means?


回答1:


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.

So you'd have threading.Thread(target=send, args=(s,)).start()

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



来源:https://stackoverflow.com/questions/36387596/python-typeerror-argument-after-must-be-a-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!