gpt4 book ai didi

python - pickle 对象实例给出 TypeError : __new__() missing required positional arguments

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:51 24 4
gpt4 key购买 nike

我正在用 Python 创建棋盘游戏,它要求我发送由作为元组子类的自定义 Hex() 类创建的对象。我面临的问题是,当服务器收到 pickle 消息(包含字符串和十六进制对象的元组)时,它会抛出以下错误:

TypeError: __new__() 缺少 2 个必需的位置参数:'r' 和 's'

我对 OOP 比较陌生,但我相信服务器在解包元组消息时会尝试创建 Hex 对象,但显然它没有重新创建原始对象所需的信息,因为它已全部打包在对象本身中。

如果有人可以建议我可以成功地将 Hex 对象从客户端发送到服务器并返回,我们将不胜感激。

代码如下:

server.py

import socket
import pickle

HOST = '127.0.0.1'
PORT = 57343
SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SOCK.bind((HOST, PORT))
SOCK.listen()

print('Server active, waiting for connections...')


conn, addr = SOCK.accept()
print('Connected to:', addr)
conn.send(pickle.dumps('conn test'))
print('test message sent to', conn)

while True:
try:
print('waiting for data')
data = pickle.loads(conn.recv(2048))
command, info = data
if command == 'move':
print('Received message', data)
elif command == 'get':
pass
conn.sendall(pickle.dumps('game'))
except (EOFError, ConnectionResetError) as err:
print(err)
break
print('Lost connection')
conn.close()
client.py

import tmp_hex_lib as hl
from network import Network

def main():
game_over = False
moves = []
attack_switch = False
net = Network()
net.get_player()
while not game_over: # main game loop
try:
net.send(('get', None))
except:
game_over = True
print("Couldn't find game")
break

selected_hex = hl.Hex(-13, 10, 3)
print('selected_hex has type:', type(selected_hex))

moves.append(selected_hex)
if attack_switch:
net.send(('attack', None))
else:
net.send(('move', 'this message works'))
net.send(('move', selected_hex)) # if any other type is sent, game runs fine
attack_switch = False

if __name__ == '__main__':
main()
network.py

import socket
import pickle

class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = "127.0.0.1"
self.port = 57343
self.addr = (self.server, self.port)
self.player = self.connect()

def get_player(self):
return self.player

def connect(self):
try:
self.client.connect(self.addr)
print('connecting to server')
data = pickle.loads(self.client.recv(2048))
print('Received data:', data)
return data
except EOFError as err:
print(err)

def send(self, data):
try:
self.client.send(pickle.dumps(data))
return pickle.loads(self.client.recv(2048))
except socket.error as err:
print(err)
tmp_hex_lib.py

class Hex(tuple):
def __new__(self, q, r, s):
return tuple.__new__(self, (q, r, s))

def __init__(self, q, r, s):
self.q = q
self.r = r
self.s = s
assert not (round(q + r + s) != 0), "q + r + s must be 0"

最佳答案

于是在查阅了pickle文档后,找到了一段pickling class instances的内容。看来,如果你正在定义自己的 __new__() 方法,你还必须定义一个方法 __getnewargs__(self) 来返回 __new__( ) 作为一个元组。例如(没有双关语意)我的情况是:

tmp_hex_lib.py

class Hex(tuple):
def __new__(self, q, r, s):
return tuple.__new__(self, (q, r, s))

def __getnewargs__(self):
return self.q self.r, self.s

def __init__(self, q, r, s):
self.q = q
self.r = r
self.s = s
assert not (round(q + r + s) != 0), "q + r + s must be 0"

希望这对其他人有帮助!

关于python - pickle 对象实例给出 TypeError : __new__() missing required positional arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56518913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com