Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use super() with arguments in Python 2.7? [duplicate]

I am asking this question because every single other page on SO I've Googled on the subject seems to devolve into overly simple cases or random discussions on details beyond the scope.

class Base:
    def __init__(self, arg1, arg2, arg3, arg4):
        self.arg1 = arg1
        self.arg2 = arg2
        self.arg3 = arg3
        self.arg4 = arg4

class Child(Base):
    def __init__(self, arg1, arg2, arg3, arg4, arg5):
        super(Child).__init__(arg1, arg2, arg3, arg4)
        self.arg5 = arg5

This is what I've tried. I get told that I need to use a type and not a classobj, but I don't know what that means and Google isn't helping.

I just want it to work. What is the correct practice in doing so in Python 2.7?

like image 567
user6596353 Avatar asked Oct 15 '25 21:10

user6596353


1 Answers

Your base class must inherit from object (new style class) and the call to super should be super(Child, self).

You should also not pass arg5 to Base's __init__.

class Base(object):
    def __init__(self, arg1, arg2, arg3, arg4):
        self.arg1 = arg1
        self.arg2 = arg2
        self.arg3 = arg3
        self.arg4 = arg4

class Child(Base):
    def __init__(self, arg1, arg2, arg3, arg4, arg5):
        super(Child, self).__init__(arg1, arg2, arg3, arg4)
        self.arg5 = arg5
like image 79
DeepSpace Avatar answered Oct 17 '25 11:10

DeepSpace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!