How to Create a Parent Class in Python


In this example we will show how to create a parent class in Python.

Source Code

# any class can be a parent class, so the syntax is the same as creating any other class
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)


p = Person('John', 15)     # create an object
p.print_person_info()      # execute the print_person_info method

Output:

name: John age: 15
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments