I have the Family
and its inherited Person
classes. How do I get the familyName
attribute from the Person
class?
class Family(object):
def __init__(self, familyName):
self.familyName = familyName
class Person(Family):
def __init__(self, personName):
self.personName = personName
For instance, let these Family
and Person
objects:
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?
Accessing Parent Class Functions This is really simple, you just have to call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class.
Classes called child classes or subclasses inherit methods and variables from parent classes or base classes. We can think of a parent class called Parent that has class variables for last_name , height , and eye_color that the child class Child will inherit from the Parent .
The child class inherits the attributes and functions of its parent class. If we have several similar classes, we can define the common functionalities of them in one class and define child classes of this parent class and implement specific functionalities there.
You cannot.
Instances only inherit the parent class methods and attributes, not instance attributes. You should not confuse the two.
strauss.familyName
is an instance attribute of a Family
instance. The Person
instances would have their own copies of the familyName
attribute.
You normally would code the Person
constructor to take two arguments:
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 Person
to hold a reference to a Family
instance:
class Person(object): def __init__(self, personName, family): self.personName = personName self.family = family
where Person
no longer inherits from Family
. Use it like:
strauss = Family('Strauss') johaness = Person('Johaness', strauss) richard = Person('Richard', strauss) print johaness.family.familyName
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With