Class

Python is an object oriented programming language. An objective can be generated from a class. The objective that is generated from a class is called an instance of the class.

In a class, we can define attributes and methods. Attributes are variables that are associated with the class. Methods are functions that are associated with the class.

Attributes

Attributes are variables that are associated with the class. We can define attributes in the class using the __init__ method. In the following example, we define a class Person with two attributes name and age.

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

In the __init__ method, we define the attributes name and age. The self keyword is used to refer to the instance of the class.

After defining the class, we can create an instance of the class.

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

person1 = Person("Alice", 25)
person2 = Person("Bob", 30)

print(person1.name) # Alice
print(person2.age) # 30

Methods

Methods are functions that are associated with the class. We can define methods in the class. In the following example, we define a class Person with a method greet.

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

    def greet(self):
        print(f"Hello, my name is {self.name}")

In the greet method, we define a method that prints a greeting message. After create an instance of the class, we can call the method.

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

    def greet(self):
        print(f"Hello, my name is {self.name}")

person1 = Person("Alice", 25)
person1.greet() # Hello, my name is Alice

We can define more methods in the class. In the following example, we define a method grow that increases the age of the person by 1.

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

    def greet(self):
        print(f"Hello, my name is {self.name}")

    def grow(self):
        self.age += 1

person1 = Person("Alice", 25)
print("Before growing:", person1.age) # 25
person1.grow()
print("After growing:", person1.age) # 26

Inheritance

Inheritance is a mechanism that allows a class to inherit attributes and methods from another class. In the following example, we define a class Student that inherits from the class Person. The instance of the Student class has the attributes and methods of the Person class. In the Student class, we use pass to indicate that the class has no other attributes or methods.

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

    def greet(self):
        print(f"Hello, my name is {self.name}")

class Student(Person):
    pass

student1 = Student("Alice", 25)
student1.greet() # Hello, my name is Alice

If we want to add more attributes to the Student class, we can define the __init__ method in the Student class. In the following example, we define a new attribute student_id in the Student class.

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

    def greet(self):
        print(f"Hello, my name is {self.name}")

class Student(Person):
    def __init__(self, name, age, student_id):
        super().__init__(name, age)
        self.student_id = student_id
    
student1 = Student("Alice", 25, 'p024325')
student1.greet() # Hello, my name is Alice
print("Student ID:", student1.student_id) # p024325

Since we defined the __init__ method in the Student class, the child class Student will not inherit the __init__ method from the parent class Person. We need to call the __init__ method of the parent class Person using the super() function.

Last updated