Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add arguments to a subclass in Python 3

class Wolf:
    def __init__(self, legs):
        self.legs = 4
class Dog(Wolf):
    def __init__(self, color):
        self.color = color
fido = Dog(legs = 4, color = "brown")

This would spute out an error message. How would I do something like that where I add parameters to the subclass that doesn't pertain to the superclass.

like image 890
Darth Vador Avatar asked Oct 22 '25 04:10

Darth Vador


1 Answers

Try this:

class Wolf:
    def __init__(self, legs):
        self.legs = 4
class Dog(Wolf):
    def __init__(self, legs, color):
        super().__init__(legs)
        self.color = color
fido = Dog(legs=4, color="brown")
like image 173
zero0 Avatar answered Oct 23 '25 20:10

zero0



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!