How to call parent class init with default values from child class?

时光总嘲笑我的痴心妄想 提交于 2020-06-16 16:10:32

问题


I am trying to give my subclasses default variables and stop duplicating code as the file is getting fat:

class Base(object):

    def __init__(self, username=None, password=None, start_url=None):
        self.username  = username
        self.password  = password
        self.start_url = start_url

class Subclass(Base):

    def __init__(self, username="hoss_it87", password="whatsgoodSO", start_url="www.boss-sauce.com"):
        super(Subclass, self).__init__()

Base works of course, but I want Subclass to init the same way, just overriding the None with real strings (all login styles are similar of course).

In [12]: x = Base('myuser', 'mypassword', 'www.google.com')

In [13]: x.username
Out[13]: 'myuser'

In [14]: x.start_url
Out[14]: 'www.google.com'

In [15]: y = Subclass()

In [16]: y.username

In [17]: y.start_url

In [18]: y.password

In [19]: 

Why do I get nothing for Subclass attributes, and how can I fix it? Thank you

Please note removing base class defaults causes other issues

class Base(object):

    def __init__(self, username, password, start_url):
        self.username  = username
        self.password  = password
        self.start_url = start_url

class Subclass(Base):

    def __init__(self, username="honeybooboosbiggestfan", password="toomuchburkerking", start_url="www.imjokingiprefericeroadtruckers.com"):
        super(Subclass, self).__init__()


In [24]: y = Subclass()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-55b9b4fdf93b> in <module>()
----> 1 y = Subclass()

<ipython-input-23-a9dc62450db7> in __init__(self, username, password, start_url)
      9 
     10     def __init__(self, username="honeybooboosbiggestfan", password="toomuchburkerking", start_url="www.imjokingiprefericeroadtruckers.com"):
---> 11         super(Subclass, self).__init__()

TypeError: __init__() takes exactly 4 arguments (1 given)

In [25]: 

Solved:

class Base(object):

    def __init__(self, username, password, start_url):
        self.username  = username
        self.password  = password
        self.start_url = start_url

class Subclass(Base):

    def __init__(self, username="honeybooboosbiggestfan", password="toomuchburkerking", start_url="www.imjokingiprefericeroadtruckers.com"):
        super(Subclass, self).__init__(username=username, password=password, start_url=start_url)

## -- End pasted text --

In [26]: y = Subclass()

In [27]: y.username
Out[27]: 'honeybooboosbiggestfan'

In [28]: 

回答1:


You have to pass the arguments from the SubClass init to the init block of Base:

class Subclass(Base):

    def __init__(self, username="hoss_it87", password="whatsgoodSO", start_url="www.boss-sauce.com"):
        super(Subclass, self).__init__(username=username, password=password, start_url=start_url)

In your code, the Base init is called without arguments and thus takes its default arguments, which are all None.



来源:https://stackoverflow.com/questions/35393960/how-to-call-parent-class-init-with-default-values-from-child-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!