Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Class Inheritance: How to initialize a subclass with values not in the parent class

I'm trying to learn more about classes and OOP.

How can I have my Person class initialize with all the values of Entity but also with a value that may not be contained in the Entity class?

For example, both Person and Spirit inherit from Entity. However, only a Person would have a gender. How can I have Person initialize with gender as well?

After that, would I still be able to create an instance of Person and call describe() in the same way I've done below?

class Entity(object):

    def __init__(self, state, name, age):
        self.state = state
        self.name = name
        self.age = age

class Person(Entity):

    def describe(self):
        print "Identification: %s, %s, %s." % (self.state, self.name, self.age)

class Spirit(Entity):
    pass  # for now

steve = Person("human", "Steve", "23" # can I then list gender here?)
steve.describe()
like image 869
pez Avatar asked May 04 '14 03:05

pez


People also ask

How do you create a class that inherits from another class?

To create a class that inherits from another class, after the class name you'll put parentheses and then list any classes that your class inherits from. In a function definition, parentheses after the function name represent arguments that the function accepts.

What is super () __ init __?

The “__init__” is a reserved method in python classes. It is known as a constructor in Object-Oriented terminology. This method when called, allows the class to initialize the attributes of the class. Python super() The super() function allows us to avoid using the base class name explicitly.

Does subclass need init?

No it isn't necessary.

Can you call parent class without its instance creation in Python?

Calling Parent class Method Well this can done using Python. You just have to create an object of the child class and call the function of the parent class using dot(.) operator.


2 Answers

Create a custom initializer on the sub-class and then call the parent class's initializer via super:

class Person(Entity):
    def __init__(self, state, name, age, gender):
        self.gender = gender
        super(Person, self).__init__(state, name, age)
like image 77
Matthew Trevor Avatar answered Sep 17 '22 21:09

Matthew Trevor


Transitionally, it looks like versions of Py 3.x (not sure which ones) allow this terse version of super():

def __init__(self, state, name, age, gender):
        self.gender = gender
        # Prototype initialization 3.x:
        super().__init__(state, name, age)

Been experimenting with SQLAlchemy models using dataclasses, so when I zipped on by looking at all things Python inheritance, I felt this might extend the answer:

from dataclasses import dataclass

@dataclass
class Entity():
    state: str
    name: str
    age: int

@dataclass
class Person(Entity):
    gender: str

    def describe(self):
        print("State: {state}, Name: {name}, Age: {age}, Gender: {gender}"
            .format(state=self.state, name=self.name, age=self.age, gender=self.gender))

man = Person("human", "humanname", 21, "cisgendered")

man.describe()
like image 29
Tim Pozza Avatar answered Sep 17 '22 21:09

Tim Pozza