@classmethod
decoratorcls
keywordsuper()
Decorators give us an easy way to extend a method's functionality without altering the method.
They take in a function as an argument, and return a function.
The "pie", @decorator
, syntax makes decorators easier to implement in our code.
Every object instantiated from the class has read and write access to its class variables.
class Pet:
total_pets = 0
all = []
class Pet:
total_pets = 0
all = []
@classmethod
def add_to_total_pets(cls):
cls.total_pets += 1
Pet.add_to_total_pets()
Heirarchical inheritance with common and differentiated attributes
class Vehicle:
def vehicle_info(self):
print("Inside Vehicle Class")
def max_speed(self):
print("max speed is 100 Km/Hour")
class Car(Vehicle):
def car_info(self):
print("Inside Car Class")
# overridden the implementation of Vehicle class
def max_speed(self):
print("max speed is 200 Km/Hour")
# Creating object of Car class
car = Car()
car.vehicle_info() # => "Inside Vehicle Class"
car.car_info() # => "Inside Car Class"
car.max_speed() # => "max speed is 200 Km/Hour"
super()
methodThe super function returns a temporary object of the parent class that allows us to call a parent class method inside a child class method.
super()
functionsuper()
function in both single and multiple inheritances.super()
function support code reusability as there is no need to write the entire functionclass Company:
def company_name(self):
return 'Google'
class Employee(Company):
def info(self):
# Calling the superclass method using super()function
c_name = super().company_name()
print("Jessa works at", c_name)
# Creating object of child class
emp = Employee()
emp.info()