pythonsuperclass-variables

Getting private attribute in parent class using super(), outside of a method


I have a class with a private constant _BAR = object().

In a child class, outside of a method (no access to self), I want to refer to _BAR.

Here is a contrived example:

class Foo:
    _BAR = object()

    def __init__(self, bar: object = _BAR):
        ...

class DFoo(Foo):
    """Child class where I want to access private class variable from parent."""

    def __init__(self, baz: object = super()._BAR):
        super().__init__(baz)

Unfortunately, this doesn't work. One gets an error: RuntimeError: super(): no arguments

Is there a way to use super outside of a method to get a parent class attribute?


The workaround is to use Foo._BAR, I am wondering though if one can use super to solve this problem.


Solution

  • Short answer: you can't !

    I'm not going into much details about super class itself here. (I've written a pure Python implementation in this gist if you like to read.)

    But now let's see how we can call super:

    1- Without arguments:

    From PEP 3135:

    This PEP proposes syntactic sugar for use of the super type to automatically construct instances of the super type binding to the class that a method was defined in, and the instance (or class object for classmethods) that the method is currently acting upon.

    The new syntax:

    super()
    

    is equivalent to:

    super(__class__, <firstarg>)
    

    ...and <firstarg> is the first parameter of the method

    So this is not an option because you don't have access to the "instance". (Body of the function/methods is not executed unless it gets called, so no problem if DFoo doesn't exist yet inside the method definition)

    2- super(type, instance)

    From documentation:

    The zero argument form only works inside a class definition, as the compiler fills in the necessary details to correctly retrieve the class being defined, as well as accessing the current instance for ordinary methods.

    What were those necessary details mentioned above? A "type" and A "instance":

    We can't pass neither "instance" nor "type" which is DFoo here. The first one is because it's not inside the method so we don't have access to instance(self). Second one is DFoo itself. By the time the body of the DFoo class is being executed there is no reference to DFoo, it doesn't exist yet. The body of the class is executed inside a namespace which is a dictionary. After that a new instance of type type which is here named DFoo is created using that populated dictionary and added to the global namespaces. That's what class keyword roughly does in its simple form.

    3- super(type, type):

    If the second argument is a type, issubclass(type2, type) must be true

    Same reason mentioned in above about accessing the DFoo.

    4- super(type):

    If the second argument is omitted, the super object returned is unbound.

    If you have an unbound super object you can't do lookup(unless for the super object's attributes itself). Remember super() object is a descriptor. You can turn an unbound object to a bound object by calling __get__ and passing the instance:

    class A:
        a = 1
    
    class B(A):
        pass
    
    class C(B):
        sup = super(B)
        try:
            sup.a
        except AttributeError as e:
            print(e)  # 'super' object has no attribute 'a'
    
    obj = C()
    print(obj.sup.a)  # 1
    

    obj.sup automatically calls the __get__.

    And again same reason about accessing DFoo type mentioned above, nothing changed. Just added for records. These are the ways how we can call super.