7

There are numerous questions on the usage of super() but none of them appears to answer my question.

When calling super().__init__() from a subclass, all method calls in the super-constructor are actually taken from the subclass. Consider the following class structure:

class A(object):
    def __init__(self):
        print("initializing A")
        self.a()
    def a(self):
        print("A.a()")

class B(A):
    def __init__(self):
        super().__init__()
        # add stuff for B
        self.bnum=3 # required by B.a()        
    def a(self):
        print("B.a(), bnum=%i"%self.bnum)

b=B()

which fails with

initializing A
Traceback (most recent call last):
  File "classmagic.py", line 17, in 
    b=B()
  File "classmagic.py", line 11, in __init__
    super().__init__()
  File "classmagic.py", line 5, in __init__
    self.a()
  File "classmagic.py", line 15, in a
    print("B.a(), bnum=%i"%self.bnum)
AttributeError: 'B' object has no attribute 'bnum'

Here I call the super constructor in B() to initialize some basic structure (some of which is executed as an own function a()). However, if I override the a() function as well, this implementation is used when calling A's constructor which fails because A knows nothing about B and may use different internal variables.

This may or may not be intuitive, but what do I have to do when I want all methods in A only to have access to the functions implemented there?

4 Answers 4

6

If your code has to call specific private methods that cannot be overridden, use a name that starts with two underscores:

class A(object):
    def __init__(self):
        print("initializing A")
        self.__a()
    def __a(self):
        print("A.a()")

class B(A):
    def __init__(self):
        super().__init__()
        # add stuff for B
        self.bnum=3 # required by B.a()        
    def __a(self):
        print("B.__a(), bnum=%i"%self.bnum)

Python "mangles" such method names by adding in the class name (plus an underscore) to minimize the chances subclasses overwrite them with their own versions.

The PEP 8 Python Style Guide has this to say about private name mangling:

If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores. This invokes Python's name mangling algorithm, where the name of the class is mangled into the attribute name. This helps avoid attribute name collisions should subclasses inadvertently contain attributes with the same name.

Note 1: Note that only the simple class name is used in the mangled name, so if a subclass chooses both the same class name and attribute name, you can still get name collisions.

Note 2: Name mangling can make certain uses, such as debugging and __getattr__(), less convenient. However the name mangling algorithm is well documented and easy to perform manually.

Note 3: Not everyone likes name mangling. Try to balance the need to avoid accidental name clashes with potential use by advanced callers.

2
  • thanks for showing __mangling, I thought the leading underscores were just convention
    – thias
    Sep 18, 2012 at 13:52
  • @thias: if there are leading and trailing double underscores, no mangling takes place; methods with both leading and trailing underscores are 'magic' methods, hooks, and such, and are reserved for python itself mostly. Think __init__, __getattr__ etc. Just to be explicit. :-)
    – Martijn Pieters
    Sep 18, 2012 at 13:54
3

Rather than calling self.a(), you'll need to use A.a(self). But this is not common to do for all methods on a particular class and you should really re-evaluate whether B should inherit from A.

3
  • that requires changing A's implementation, which I wanted to avoid but thanks for pointing this out
    – thias
    Sep 18, 2012 at 13:53
  • @thias -- As far as I see it, you need to change the implementation of A no matter what (whether by name mangling as suggested by Martijn, or by specifying explicitly which method you're calling)
    – mgilson
    Sep 18, 2012 at 13:56
  • it's true. I do it now by calling the __a function in the constructor and implement an a() function that simply calls __a and can be overridden. That way, I have a consistent external interface but am still able to call __a from A.__init__.
    – thias
    Sep 18, 2012 at 14:11
0

Consider this call:

class B(A):
    def __init__(self):
        A.__init__(self)

which is what happens when you call super().__init__(). This, in turn, calls self.a(), which is of course the function a of class B and not A because self is of class B. As Martijn stated, you can use dual-underscore names, or explicitly use the class name, but otherwise it is impossible to call an overridden method from a superclass.

0

If we go through the instanciation steps of B:

  • We call super(B,self).__init__, that is, A.__init__
  • which calls self.a(), that is, B.a() in our case,
  • which uses self.bnum

Except that bnum hasn't been defined yet... So, AttributeError.

For this particular case, it's just enough to define your bnum in B.__init__ before calling super(B,self).__init__

class B(A):
    def __init__(self):
       self.bnum=3
       super(B, self).__init__()

Before going the dark, dark path of name mangling, you may want to just take the time to organize the code of your subclass: should it be performed before the parent's, to initialize some special variables, or after the parent's, to replace some defaults ?

3
  • I can see that. My real problem is a little more involved, though (the implementation in the subclass uses a module that is not imported in the superclass namespace).
    – thias
    Sep 18, 2012 at 13:54
  • I think he doesn't want B.a to get called at all during the super call, so while declaring bnum before calling super doesn't throw an AttributeError anymore it is not actually a solution for his problem.
    – l4mpi
    Sep 18, 2012 at 13:55
  • Ah. So, is super(B,self).__init__ needed at all ?
    – Pierre GM
    Sep 18, 2012 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.