簡體   English   中英

如何修復 - TypeError:無法設置內置/擴展類型“set”的屬性

[英]How to fix - TypeError: can't set attributes of built-in/extension type 'set'

我對 python 比較陌生,我正在嘗試與 python 進行簡單的 GUI 聊天。 它被編程為在客戶端加入服務器時詢問昵稱。 一切正常,直到我輸入昵稱的部分。 當我輸入昵稱時,我分別從服務器和客戶端收到這些錯誤,我也會提供回溯。

ConnectionResetError: [Errno 104] Connection reset by peer (來自服務器)

追溯:

Traceback (most recent call last):
  File "server.py", line 51, in <module>
    receive()
  File "server.py", line 44, in receive
    broadcast(f"{nickname} entered to the chat!\n".encode('utf-8'))
  File "server.py", line 17, in broadcast
    client.send(message)

TypeError: can't set attributes of built-in/extension type 'set' (來自客戶端)

追溯:

Traceback (most recent call last):
  File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 94, in <module>
    client = Client(HOST, PORT)
  File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 22, in __init__
    set.gui_done = False

服務器和客戶端的代碼也是鏈接的。

服務器-> https://pastebin.com/0W7Cw9Cu

客戶端-> https://pastebin.com/FES2UNc1

我試過的:

我嘗試在谷歌上搜索答案,但我不能說我沒有得到任何答案,但我不明白如何為我的問題實施這些解決方案。 這些是我提到的鏈接

  1. Python 處理 socket.error: [Errno 104] Connection reset by peer

  2. python 無法設置內置/擴展類型“對象”的屬性

我認為錯誤消息非常准確地指出了問題所在。

Traceback (most recent call last):
  File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 94, in <module>
    client = Client(HOST, PORT)
  File "C:/Users/ISINDU WICKRAMASEKAR/PycharmProjects/guichat/client.py", line 22, in __init__
    set.gui_done = False
TypeError: can't set attributes of built-in/extension type 'set' (from client)

在 client.py 的第 22 行,您嘗試分配set.gui_done = False 您可能的意思是self.gui_done

你的問題是這條線

set.gui_done = False

set是 python 中的受保護關鍵字。 它是為set數據類型保留的。 通過這一行,您告訴解釋器將內置set類型的gui_done屬性更改為 false,這是不允許的。 這就是你得到TypeError的原因。 如果它不是內置類型,您仍然會收到AttributeError ,因為set沒有gui_done屬性。 您可能打算使用self而不是set

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM