English 中文(简体)
遗产继承
原标题:The inheritance of attributes using __init__

刚开始学习的Im Java人 举这个例子:

class Person():
    def __init__(self, name, phone):
        self.name = name
        self.phone = phone

class Teenager(Person):
    def __init__(self, name, phone, website):
        self.name=name
        self.phone=phone
        self.website=website

我确信,有许多多余的法典(我知道,在 Java,上文法典的范围有许多重复之处)。

哪些部分是多余的,涉及哪些属性已经从母阶级继承?

最佳回答

在撰写<代码>_init__时,您应始终称其超流层的_init_功能。 我们可以利用这一方法将相关属性直接传递给超级阶级,因此,你的法典希望:

class Person(object):
    def __init__(self, name, phone):
        self.name = name
        self.phone = phone
class Teenager(Person):
    def __init__(self, name, phone, website):
        Person.__init__(self, name, phone)
        self.website=website

正如其他人所指出的,你可以取代这条线。

Person.__init__(self, name, phone)

iii

super(Teenager, self).__init__(name, phone)

守则也将这样做。 这是因为,在python instance.method(args)上,对Class.method(instance, args)只字不提。 如果您希望使用<条码>,<>代号> 如我在守则中所做的那样,需要确保你将<条码>目标作为<条码>的基础类别。

python documentation 关于如何使用<代码><<>super关键词的信息。 本案中的重要内容是,它把研究<条码>_init_<>/code>的方法(<条码>)放在<条码>上。 页: 1

问题回答

更清洁地说,我要这样做:

class Teenager(Person):
        def __init__(self, *args, **kwargs):
           self.website=kwargs.pop( website )
           super(Teenager, self).__init__(*args, **kwargs)

在本案中,它没有什么不同,但当你有<>条码>_init__,有一线论据时,它使生活更加容易。

至今,所有例例都为2. ́x,但在此为3. 谷物找到解决办法,使用较短版本的 super和 does的 t。

class Person:
    def __init__(self, name, phone):
        self.name = name
        self.phone = phone

class Teenager(Person):
    def __init__(self, name, phone, website):
        super().__init__(name, phone)
        self.website = website

关于在涉及许多案例属性时建议采用更清洁的解决办法的@Tom回答,另一种做法是,例如,在添加<条码>时,即:

class Person():
    def __init__(self, name, phone):
        self.name = name
        self.phone = phone

class Teenager(Person):
    def __init__(self, website, *args, **kwargs):
        self.website = website
        super().__init__(*args, **kwargs)

if __name__ ==  __main__ :
    chris = Teenager( www.chris.com ,  chris ,  802-250-5159 )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签