self vs super

 Self vs Super

  • self is used to refer to the current object that is created or being instantiated, while super is used to refer to the parent class.
  • self is used when there is a need to differentiate between the instance variables & parameters with the same name, while super is used to call the parent class methods and/or constructors.

Coding example:


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


class Student(Person):
    def __init__(self, name, age, degree):
        super().__init__(name=name, age=age) -> calling parent constructure.
        self.degree = degree





Comments

Popular posts from this blog

Post Request with Pydantic usage (input validator)

Code Scalability and Routing

CRUD Assignment