How to Add a Property to Child Class in Python


In this example we will show how to add a property to the child class in Python.

Source Code

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

    def print_person_info(self):
        print('name:', self.name, 'age:', self.age)


class Student(Person):
    def __init__(self, name, age):
        super().__init__(name, age)
        self.grade = 'A'


s = Student('John', 15)
print(s.grade)

Output:

A
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

How to Add a Property to Child Class in Python


In this example we will show how to add a property to the child class by adding another parameter in the __init__() function in Python.

Source Code

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

    def print_person_info(self):
        print('name:', self.name, 'age:', self.age)


class Student(Person):
    def __init__(self, name, age, grade):
        super().__init__(name, age)
        self.grade = grade


s = Student('John', 15, 'A')
print(s.grade)

Output:

A
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments