descriptor '__init__' of 'super' object needs argument
我正在尝试用Python制作一个面向对象的基于文本的游戏,并尝试实现我的第一个属性和装饰器。根据"python 3面向对象编程"一书中的第5章,我尝试使用所讨论的示例和概念来获得以下代码,以便在实例化时设置游戏对象的"当前房间"属性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Room(object): ''' An area of the game's map.''' def __init__(self): print("Accessing the Room __init__ method.") class FirstRoom(Room): ''' Just some room.''' def __init__(self): print("Accessing the FirstRoom __init__ method.") super.__init__() class SecondRoom(Room): ''' Just some other room.''' def __init__(self): print("Accessing the SecondRoom __init__ method.") super.__init__() class Game(object): ''' Creates a new game.''' current_room = None # Class-level definition of this property. def __init__(self): print("Created a new Game object.") self.current_room = FirstRoom() @property def current_room(self): ''' Returns the current position of the actor.''' print("Getting the _current_room attribute for the Game object.") return self._current_room @current_room.setter def set_room(self, new_room): ''' Sets the current_room property of the Game object.''' print("Setting the _current_room attribute for the Game object.") self._current_room = new_room |
但是,当我运行此代码时,会得到以下输出:
1 2 3 4 5 6 7 8 9 10 | >>> g = Game() Created a new Game object. Accessing the FirstRoom __init__ method. Traceback (most recent call last): File"<stdin>", line 1, in <module> File"/home/drew/Desktop/test.py", line 27, in __init__ self.current_room = FirstRoom() File"/home/drew/Desktop/test.py", line 11, in __init__ super.__init__() TypeError: descriptor '__init__' of 'super' object needs an argument |
为了使这个语法正常工作,代码中缺少了什么?是否需要为我的"当前房间"属性显式定义描述符?[这本书没有提到任何关于描述符的内容,至少不像您在这里看到的那样:Python描述符被解离了。]
用
这里是工作代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class Room(object): ''' An area of the game's map.''' def __init__(self): print("Accessing the Room __init__ method.") class FirstRoom(Room): ''' Just some room.''' def __init__(self): print("Accessing the FirstRoom __init__ method.") #super.__init__() super(FirstRoom, self).__init__() class SecondRoom(Room): ''' Just some other room.''' def __init__(self): print("Accessing the SecondRoom __init__ method.") super(SecondRoom, self).__init__() #super.__init__() class Game(object): ''' Creates a new game.''' _current_room = None # Class-level definition of this property. def __init__(self): print("Created a new Game object.") self._current_room = FirstRoom() @property def current_room(self): ''' Returns the current position of the actor.''' print("Getting the _current_room attribute for the Game object.") return self._current_room @current_room.setter def set_room(self, new_room): ''' Sets the current_room property of the Game object.''' print("Setting the _current_room attribute for the Game object.") self._current_room = new_room |
运行时:
1 2 3 4 5 | >>> g = Game() Created a new Game object. Accessing the FirstRoom __init__ method. Accessing the Room __init__ method. >>> |
希望这对你有帮助。
超级错误实际上是在调用超级代码之前将打印内容放入超级代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Room(object): ''' An area of the game's map.''' def __init__(self): print("Accessing the Room __init__ method.") class FirstRoom(Room): ''' Just some room.''' def __init__(self): super().__init__() print("Accessing the FirstRoom __init__ method.") class SecondRoom(Room): ''' Just some other room.''' def __init__(self): super().__init__() print("Accessing the SecondRoom __init__ method.") |
您需要了解
python docs属性
从文档中,确保为附加函数赋予与原始属性相同的名称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Game(object): ''' Creates a new game.''' current_room = None # Class-level definition of this property. def __init__(self): print("Created a new Game object.") self._current_room = FirstRoom() @property def current_room(self, room): ''' Returns the current position of the actor.''' print("Getting the _current_room attribute for the Game object.") return self._current_room @current_room.setter # same function name as the property. def current_room(self, new_room):# same name as the property ''' Sets the current_room property of the Game object.''' print("Setting the _current_room attribute for the Game object.") self._current_room=new_room |