descriptor '__init__' of 'super' object needs argument

Automatic Bazooty picture Automatic Bazooty · May 27, 2014 · Viewed 14.1k times · Source

I'm trying my hand at making an Object-Oriented text-based game in Python, and attempting to implement my first properties and decorators. Using the chapter 5 in the book 'Python 3 Object Oriented Programming', I've tried to use the examples and concepts discussed to get the following code to set a Game-object's 'current_room' property upon instantiation:

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

However, when I run this code, I get the following output:

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

What am I missing from my code to make this syntax work? Do I need to explicitly define a Descriptor for my 'current_room' property? [The book doesn't mention anything about Descriptors, at least not like what you would find here: Python Descriptors Demystified.]

Answer

splendor picture splendor · Jun 7, 2016

use super().__init__() instead of super.__init__()