如何从 Python 中的继承类设置和获取父类属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15989217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:32:08  来源:igfitidea点击:

How to set and get a parent class attribute from an inherited class in Python?

pythonclassinheritance

提问by msampaio

I have the Familyand its inherited Personclasses. How do I get the familyNameattribute from the Personclass?

我有Family及其继承的Person类。如何familyNamePerson类中获取属性?

class Family(object):
    def __init__(self, familyName):
        self.familyName = familyName

class Person(Family):
    def __init__(self, personName):
        self.personName = personName

For instance, let these Familyand Personobjects:

例如,让这些FamilyPerson对象:

strauss = Family('Strauss')
johaness = Person('Johaness')
richard = Person('Richard')

I'd like to do something such as:

我想做一些事情,例如:

print richard.familyName

and get 'Strauss'. How can I do this?

并得到'Strauss'。我怎样才能做到这一点?

采纳答案by Martijn Pieters

You cannot.

你不能。

Instances only inherit the parent class methods and attributes, not instanceattributes. You should not confuse the two.

实例只继承父类的方法和属性,而不是实例属性。你不应该混淆两者。

strauss.familyNameis an instanceattribute of a Familyinstance. The Personinstances would have their owncopies of the familyNameattribute.

strauss.familyName实例的实例属性Family。本Person实例将有他们自己的副本familyName属性。

You normally would code the Personconstructor to take two arguments:

您通常会将Person构造函数编码为采用两个参数:

class Person(Family):
    def __init__(self, personName, familyName):
        super(Person, self).__init__(familyName)
        self.personName = personName

johaness = Person('Johaness', 'Strauss')
richard = Person('Richard', 'Strauss')

An alternative approach would be for Personto hold a reference to a Familyinstance:

另一种方法是Person保存对Family实例的引用:

class Person(object):
    def __init__(self, personName, family):
        self.personName = personName
        self.family = family

where Personno longer inherits from Family. Use it like:

其中,Person从不再继承Family。像这样使用它:

strauss = Family('Strauss')
johaness = Person('Johaness', strauss)
richard = Person('Richard', strauss)

print johaness.family.familyName

回答by Lennart Regebro

In addition to Martijns suggestions, you can also create the Person from the Family instance, that way letting the family keep track of it's members:

除了 Martijns 建议,您还可以从 Family 实例创建 Person,这样可以让家庭跟踪其成员:

class Person(object):
    def __init__(self, person_name, family):
        self.person_name = person_name
        self.family = family

    def __str__(self):
        return ' '.join((self.person_name, self.family.family_name))

class Family(object):
    def __init__(self, family_name):
        self.family_name = family_name
        self.members = []

    def add_person(self, person_name):
        person = Person(person_name, self)
        self.members.append(person)
        return person

    def __str__(self):
        return 'The %s family: ' % self.family_name + ', '.join(str(x) for x in self.members)

Usage like this:

用法如下:

>>> strauss = Family('Strauss')
>>> johannes = strauss.add_person('Johannes')
>>> richard = strauss.add_person('Richard')
>>> 
>>> print johannes
Johannes Strauss
>>> print richard
Richard Strauss
>>> print strauss
The Strauss family: Johannes Strauss, Richard Strauss