我 100% 確定這是我的錯。我敢打賭,這是非常明顯的事情。所以請相信我,當我說我在發布之前已經查看了一段時間的代碼時。我只需要有人幫助找出這些錯誤的原因,我相信我能從那里解決它。也許只是一雙新的眼睛查看代碼就足以快速發現錯誤。
使用 python 3.9
問題
我第一次創建 Command 物件時,所有輸出看起來都很好。但是如果我創建第二個 Command 物件,第一個 Command 物件的輸出就會改變。
例如,如果我運行這個:
test_command = Command("Power to comms", {REGULAR_RESOURCE_NAMES["power"]: Power(value=1)},
{REGULAR_RESOURCE_NAMES["comms"]: Comms(value=2)})
我在除錯器中得到以下輸出:
Power to comms [Command]
Input resources: {'Power': <data_structure.Power object at 0x7f17e56e78b0>}
Output resources: {'Comms': <data_structure.Comms object at 0x7f17e56e77c0>}
但是如果我之后運行這個:
test_command2 = Command("Comms and power to navs", {REGULAR_RESOURCE_NAMES["comms"]: Comms(value=2),
REGULAR_RESOURCE_NAMES["power"]: Power(value=1)},
{REGULAR_RESOURCE_NAMES["navs"]: Navs(value=5)})
我在除錯器中得到以下輸出:
Power to comms [Command]
Input resources: {'Power': <data_structure.Power object at 0x7f17e57135b0>, 'Comms': <data_structure.Comms object at 0x7f17e5713610>}
Output resources: {'Comms': <data_structure.Comms object at 0x7f17e56e77c0>, 'Navs': <data_structure.Navs object at 0x7f17e56e78b0>}
Comms and power to navs [Command]
Input resources: {'Power': <data_structure.Power object at 0x7f17e57135b0>, 'Comms': <data_structure.Comms object at 0x7f17e5713610>}
Output resources: {'Comms': <data_structure.Comms object at 0x7f17e56e77c0>, 'Navs': <data_structure.Navs object at 0x7f17e56e78b0>}
首先,我們可以看到兩個Command物件中的Power物件共享同一個地址。這不是我的本意。所以這是第一個錯誤。其次,為什么第一個命令突然有比以前更多的輸入/輸出資源?
我在下面包含了相關代碼:
我的代碼
class Command:
"""
Contains a ratio for exchanging input resources into output resources
"""
name: str
input_resources: dict[str, type(Resource)] = {}
output_resources: dict[str, type(Resource)] = {}
def __init__(self, name: str,
input_resources: dict[str, type(Resource)], output_resources: dict[str, type(Resource)]):
self.name: str = name
for input_resource_name, input_resource in input_resources.items():
self.input_resources[input_resource_name]: type(Resource) = input_resource.copy()
for output_resource_name, output_resource in output_resources.items():
self.output_resources[output_resource_name]: type(Resource) = output_resource.copy()
def __str__(self) -> str:
output: str = self.name " [Command]" "\n\t" \
"Input resources: " str(self.input_resources) "\n\t" \
"Output resources: " str(self.output_resources) "\n"
return remove_trailing_newlines(output)
def copy(self) -> type(__name__):
input_resources_copy: dict[str, type(Resource)] = {}
for input_resource_name, input_resource in self.input_resources.items():
input_resources_copy[input_resource_name]: type(Resource) = input_resource.copy()
output_resources_copy: dict[str, type(Resource)] = {}
for output_resource_name, output_resource in self.output_resources.items():
output_resources_copy[output_resource_name]: type(Resource) = output_resource.copy()
return Command(self.name, input_resources_copy, output_resources_copy)
class Resource:
"""
The base class for all resource types, such as Comms, Navs, Data, Heat, Drift, Thrust
"""
name: str
value: int
min_value: int
max_value: int
def __init__(self, name: str, value: int = 0, min_value: int = 0, max_value: int = 999):
self.name: str = name
self.value: int = value
self.min_value: int = min_value
self.max_value: int = max_value
def is_valid_value(self) -> bool:
"""
Checks if the numerical validity of the resource value based on init parameters
"""
return self.min_value <= self.value <= self.max_value
class Power(Resource):
"""
This subclass of Resource, contains variables and methods specific to this particular in-game resource.
This is what's considered a regular resource.
"""
def __init__(self, value: int = 0):
super().__init__(REGULAR_RESOURCE_NAMES["power"], value=value)
def next_turn(self) -> bool:
return self.is_valid_value()
def is_valid_end_of_route(self) -> bool:
return self.is_valid_value()
def copy(self) -> type(__name__):
return Power(value=self.value)
除了名稱之外,其他資源如 Comms、Navs、Data 與 Power 類完全相同。
以下是在全域范圍內實體化的:
REGULAR_RESOURCE_NAMES = {
"comms": "Comms",
"navs": "Navs",
"data": "Data",
"power": "Power"
}
uj5u.com熱心網友回復:
當你為這樣的類定義變數時
class Command:
"""
Contains a ratio for exchanging input resources into output resources
"""
name: str
input_resources: dict[str, type(Resource)] = {}
output_resources: dict[str, type(Resource)] = {}
這些變數被分配給類而不是實體。在您的__init__函式中,您分配給類的變數而不是實體擁有的變數。如果您洗掉類級別定義,并在__init__下面提到的 Frank 中分別初始化這些定義,它應該可以作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368883.html
上一篇:Python重新匹配字數
