AttributeError: 类对象’map’没有属性’zone_map’

huangapple go评论65阅读模式
英文:

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) -&gt; nx.Graph:
        zone_map = nx.Graph()
        zone_map.add_nodes_from(zone_data.keys())

        def create_edges():
            node = &quot;&quot;
            for node in zone_map.nodes:
                split_name = [x for x in node]
                if split_name[0] != &quot;A&quot;:
                    yield (node, chr(ord(split_name[0]) - 1) + split_name[1])
                if split_name[0] != &quot;E&quot;:
                    yield (node, chr(ord(split_name[0]) + 1) + split_name[1])
                if split_name[1] != &quot;1&quot;:
                    yield (node, split_name[0] + str(int(split_name[1]) - 1))
                if split_name[1] != &quot;5&quot;:
                    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) -&gt; 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=&quot;&quot;, is_player_here=False, map: &quot;Map&quot; = 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() # &lt;--- Gives AttributeError

Map is a class which contains the methods to create and print the map:

    def create_map(self, zone_data: dict) -&gt; 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) -&gt; 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.

huangapple
  • 本文由 发表于 2023年2月18日 22:48:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定