super'对象的'__init__'描述符需要参数

27 投票
3 回答
35260 浏览
提问于 2025-04-18 07:39

我正在尝试用Python制作一个面向对象的文字游戏,并且想要实现我的第一个属性和装饰器。我参考了书籍'Python 3 面向对象编程'中的第五章,试图使用书中讨论的例子和概念,让下面的代码在创建游戏对象时设置它的'current_room'属性:

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

但是,当我运行这段代码时,输出结果是:

>>> 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

我在代码中缺少了什么,让这个语法无法正常工作?我需要为我的'current_room'属性明确地定义一个描述符吗?[这本书没有提到描述符,至少没有像这里提到的那样:Python 描述符揭秘。]

3 个回答

0

这里是可以运行的代码:

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

运行后会得到:

>>> g = Game()
Created a new Game object.
Accessing the FirstRoom __init__ method.
Accessing the Room __init__ method.
>>> 

希望这对你有帮助。

0

这个超级错误其实是因为你在调用super之前放了print语句,下面的代码就能正常工作:

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.")

你需要了解一下@property等等是怎么工作的。

关于Property的Python文档

根据文档,确保给额外的函数起和原始属性一样的名字

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  
110

super().__init__() 代替 super.__init__()

撰写回答