英文:
AttributeError: type object 'map' has no attribute 'zone_map'
问题
I am using networkx to represent a map for my game. I have created the map and it works perfectly fine:
def create_map(self, zone_data: dict) -> nx.Graph:
zone_map = nx.Graph()
zone_map.add_nodes_from(zone_data.keys())
def create_edges():
node = ""
for node in zone_map.nodes:
split_name = [x for x in node]
if split_name[0] != "A":
yield (node, chr(ord(split_name[0]) - 1) + split_name[1])
if split_name[0] != "E":
yield (node, chr(ord(split_name[0]) + 1) + split_name[1])
if split_name[1] != "1":
yield (node, split_name[0] + str(int(split_name[1]) - 1))
if split_name[1] != "5":
yield (node, split_name[0] + str(int(split_name[1]) + 1))
zone_map.add_edges_from(create_edges())
return zone_map
I then have created a list of the nodes on the graph that a player can go to using neighbors().
def check_border(self) -> list[str]:
# * Check border by using the networkx graph created in the Map class.
moveable_zones = []
for i in map.zone_map.neighbors(self.name):
moveable_zones.append(i)
return moveable_zones
This all works just fine, but when I try to assign the result of check_border(), which is defined inside a class called Zone, to as an attribute of a class object, it returns an Attribute error saying that Map object has no attribute zone_map.
class Zone(Map):
def __init__(
self, name, description="", is_player_here=False, map: "Map" = Map(5, 5, {})
):
self.height = map.height
self.name = name
self.description = description
self.is_player_here = is_player_here
# * The following line of code returns an error
self.moveable_zones = self.check_border() # <-- Gives AttributeError
Map is a class which contains the methods to create and print the map:
def create_map(self, zone_data: dict) -> nx.Graph:
zone_map = nx.Graph()
zone_map.add_nodes_from(zone_data.keys())
It all works perfectly fine until I assign self.moveable_zones to self.check_border()
I have tried changing the check_border() method by adding zone_map as an attribute to the Zone class and using self instead of map with. I have tried using super() on Zone but that leads to multiple other errors, one saying that zone_data has no attribute keys even though it is a dictionary. I am not sure how to fix this problem.
英文:
I am using networkx to represent a map for my game. I have created the map and it works perfectly fine:
def create_map(self, zone_data: dict) -> nx.Graph:
zone_map = nx.Graph()
zone_map.add_nodes_from(zone_data.keys())
def create_edges():
node = ""
for node in zone_map.nodes:
split_name = [x for x in node]
if split_name[0] != "A":
yield (node, chr(ord(split_name[0]) - 1) + split_name[1])
if split_name[0] != "E":
yield (node, chr(ord(split_name[0]) + 1) + split_name[1])
if split_name[1] != "1":
yield (node, split_name[0] + str(int(split_name[1]) - 1))
if split_name[1] != "5":
yield (node, split_name[0] + str(int(split_name[1]) + 1))
zone_map.add_edges_from(create_edges())
return zone_map
I then have created a list of the nodes on the graph that a player can go to using neighbors().
def check_border(self) -> list[str]:
# * Check border by using the networkx graph created in the Map class.
moveable_zones = []
for i in map.zone_map.neighbors(self.name):
moveable_zones.append(i)
return moveable_zones
This all works just fine, but when I try to assign the result of check_border(), which is defined inside a class called Zone, to as an attibute of a class object, it returns an Attribute error saying that Map object has no attribute zone_map.
class Zone(Map):
def __init__(
self, name, description="", is_player_here=False, map: "Map" = Map(5, 5, {})
):
self.height = map.height
self.name = name
self.description = description
self.is_player_here = is_player_here
# * The following line of code returns an error
self.moveable_zones = self.check_border() # <--- Gives AttributeError
Map is a class which contains the methods to create and print the map:
def create_map(self, zone_data: dict) -> nx.Graph:
zone_map = nx.Graph()
zone_map.add_nodes_from(zone_data.keys())
It all works perfectly fine until I assign self.moveable_zones to self.check_border()
I have tried changing the check_border() method by adding zone_map as an attribute to the Zone class and using self instead of map with. I have tried using super() on Zone but that leads to multiple other errors, one saying that zone_data has no attribute keys even though it is a dictionary. I am not sure how to fix this problem.
答案1
得分: 1
def check_border(self) -> list[str]:
# * 使用地图类中创建的networkx图检查边界。
可移动区域 = []
for i in map.zone_map.neighbors(self.name):
可移动区域.append(i)
返回 可移动区域
英文:
def check_border(self) -> list[str]:
# * Check border by using the networkx graph created in the Map class.
moveable_zones = []
for i in map.zone_map.neighbors(self.name):
moveable_zones.append(i)
return moveable_zones
map
is not an argument to this function, nor it is a local variable.
Therefore Python uses the built-in function map()
, which is something completely different than you were expecting.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论