Search code examples
pythonsuperclass

How to override a superclass __init__ argument?


How do I override the super class attribute? In the example below, I want to use some other value for name.

class A:
    def __init__(self,name):
        self.name=name

class B(A):
    def __init__(self, config, address, phone):
        super(B, self).__init__(name)
        self.address=address
        self.phone=phone
        self.config=config
        self.name=self.config.name + "__value_only"

Solution

  • Just pass the desired "other" value to the super(...).__init__(). Also, name in your example is not a class, but an instance attribute:

    class B(A):
        def __init__(self, config, address, phone):
            super(B, self).__init__(config.name + "__value_only")
            self.address=address
            self.phone=phone
            self.config=config