我有这段代码,我尝试在新线程中发送UDP数据报
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()
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将套接字作为函数的参数时,会抛出一个TypeError异常:
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
Run Code Online (Sandbox Code Playgroud)
那意味着什么?
我正在学习如何在打字稿中使用 rest 参数/扩展运算符。现在,我需要编写一个将数组作为参数的函数,并且我想使用 rest 运算符。这是函数:
insert(elem: number, ...elems: number[])
Run Code Online (Sandbox Code Playgroud)
的elem参数是有,因为我需要在阵列具有至少一个元素。
所以,给定一个这样的数组:
const numArray = [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
如何将数组传递给函数?我已经尝试了以下但它给了我一个错误:
insert(...numArray)
Run Code Online (Sandbox Code Playgroud)
我理解错误,因为 numArray 可能有 0 到 N 个元素,并且该函数至少需要一个元素,但我不知道最佳解决方案。
有没有办法实现这一目标?
注意:该insert函数是我正在开发的库的一部分,因此我需要使其尽可能可用,而不取决于用户将如何使用它
我在Haskell中有以下代码:
module Shape where
type Height = Float
type Width = Float
type Radius = Float
data Rectangle = Rectangle Height Width
data Circle = Circle Radius
class (Eq a, Show a) => Shape a where
area :: a -> Float
perimeter :: a -> Float
instance Shape Rectangle where
area (Rectangle h w) = h * w
perimeter (Rectangle h w) = h*2 + w*2
show (Rectangle h w) = "rectangle " ++ (show h) ++ " " ++ …Run Code Online (Sandbox Code Playgroud) 我只是试图打印键和字典的值,但我得到了TypeError.代码:
def __str__(self):
string = ""
for key in self.dictionary:
string += key, "-->", self.dictionary[key] + '\n'
return string
Run Code Online (Sandbox Code Playgroud)
我添加了例如键'key'和值'value',字典的内容是正确的:
{'key': 'value'}
Run Code Online (Sandbox Code Playgroud)
但后来我尝试调用str方法并得到这个:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "dictionary.py", line 37, in list
print self.__str__()
File "dictionary.py", line 42, in __str__
string += key, "-->", self.dictionary[key] + '\n'
TypeError: cannot concatenate 'str' and 'tuple' objects
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会出现这个错误,关键是字符串就像值一样
python ×2
typeerror ×2
class ×1
dictionary ×1
haskell ×1
instance ×1
parameters ×1
typescript ×1