AttributeError: 'super' object has no attribute 'init'

Issue #131 resolved
Tzu-ping Chung
created an issue

OS X 10.10.4, Python 2.7.10

Running the example from the introduction:

from Foundation import NSObject


class MyClass(NSObject):

    def init(self):
        """
        Designated initializer for MyClass
        """
        # ALWAYS call the super's designated initializer.
        # Also, make sure to re-bind "self" just in case it
        # returns something else, or even None!
        self = super(MyClass, self).init()
        if self is None:
            return None

        self.myVariable = 10

        # Unlike Python's __init__, initializers MUST return self,
        # because they are allowed to return any object!
        return self


class MyOtherClass(MyClass):

    def initWithOtherVariable_(self, otherVariable):
        """
        Designated initializer for MyOtherClass
        """
        self = super(MyOtherClass, self).init()
        if self is None:
            return None

        self.otherVariable = otherVariable
        return self


myInstance = MyClass.alloc().init()
myOtherInstance = MyOtherClass.alloc().initWithOtherVariable_(20)

Result:

$ python test.py
Traceback (most recent call last):
  File "test.py", line 38, in <module>
    myInstance = MyClass.alloc().init()
  File "test.py", line 13, in init
    self = super(MyClass, self).init()
AttributeError: 'super' object has no attribute 'init'

Comments (3)

  1. Ronald Oussoren repo owner

    You need to add "from objc import super" to the top of the module. The builtin version of super doesn't work properly with Cocoa classes.

    I'm working on a PEP that enhances the builtin super and takes away the need for the import. With some luck that will be included in Python 3.6

    Ronald

    -- On the road, hence brief.

  2. Ronald Oussoren repo owner

    This is not a bug, as mentioned earlier you need to use "from objc import super" to get a working version of super.

    The reason for this is that builtin.super assumes that classes are static and that it is sufficient to peek in the dict of classes.

    I have written a PEP to fix this in a future release of Python, although that PEP is making slow progress due to lack of time on my end.

  3. Log in to comment