簡體   English   中英

我不知道為什么這個 AttributeError 持續存在

[英]I can't figure out why this AttributeError is persisting

你好 Stack Overflow 的人 [我的第一篇文章,請原諒問題結構中的任何特質,等等屬性錯誤 [字典中的庄園 class 引用只有一個 function def enter(self):

非常感謝任何建議/提示/解釋,謝謝!

這是輸入的代碼:

class Manor(Scenes):
"""First encounter class."""

def enter(self):
    """First encounter."""
    print("""plot fodder""")

class Map(object):
    """The Map w/ a data dictionary using classes."""

    scenes = {
        'manor': Manor(),
        'master_bedroom': MasterBedroom(),
        'dining_room': DiningRoom(),
        'escape_scene': EscapeTheHouse(),
        'death': Death(),
        'finished': Finished()
    }

    def __init__(self, start_scene):
        """Instance initiator."""
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        """Move to next class."""
        value = Map.scenes.get(scene_name)
        return value

    def opening_scene(self):
        """Mechanism that starts."""
        return self.next_scene(self.start_scene)


class Engine(object):
    """Engine of the game class."""

    def __init__(self, scene_map):
        """instance."""
        self.scene_map = scene_map

    def play(self):
        """Engine of the class."""
        current_scene = self.scene_map.opening_scene()
        last_scene = self.scene_map.next_scene('finished')

        while current_scene != last_scene:
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

        current_scene.enter()


a_map = Map.scenes[('manor')]
a_game = Engine(a_map)
a_game.play()

這是 powershell 錯誤: line 159, in play current_scene = self.scene_map.opening_scene() AttributeError: 'Manor' object has no attribute 'opening_scene'

一個

class 在 function 中包含多個條件分支,但我認為它可能不在問題的 scope 范圍內,如果不是很好,我會補充

如果我沒有提供更多信息,請告訴我!

您將Map.scenes[('manor')]分配給a_map

a_map = Map.scenes[('manor')]

這是Manor()的一個實例,定義如下:

scenes = {
        'manor': Manor(),
        # ...
}

然后,您將Manor()的實例傳遞給Engine()構造函數:

a_game = Engine(a_map)

它在這里將其分配給self.scene_map

def __init__(self, scene_map):
        """instance."""
        self.scene_map = scene_map

然后你調用.play() ,它會這樣做:

current_scene = self.scene_map.opening_scene()

因此,它試圖在Manor()的實例上調用.opening_scene() ) ,但Manor沒有opening_scene方法,所以它失敗了。

暫無
暫無

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

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