Inheritance in Python (1st Method)

class Phone:
    def __init__(self, brand, model_name, price):
        self.brand = brand
        self.model_name = model_name
        self.price = price
        
    @property
    def full_name(self):
        return f"This phone is {self.brand} {self.model_name}"
    
    def make_a_call(self, number):
        return f"Dialling {number}..."

class Smartphone(Phone):
    def __init__(self, brand, model_name, price, ram, internal_memory, rear_camera):
        super().__init__(brand, model_name, price)
        self.ram = ram
        self.internal_memory = internal_memory
        self.rear_camera = rear_camera
    
S1 = Smartphone("OnePlus", "8 Pro", "75000", "8GB", "256GB", "64MP")

print(S1.full_name)

Implementing static method in Python

class Laptop():
    discount = 10
    def __init__(self, brand_name, model_name, price):
        #initializing instance varaibles
        self.bn = brand_name
        self.mn = model_name
        self.p = price

    def apply_disc(self):
        after_disc = self.p - (self.p * (Laptop.discount/100))
        return after_disc

    def apply_special_disc(self, disc):
        after_spcl_disc = self.p - (self.p * (disc/100))
        return after_spcl_disc
    @classmethod
    def from_string(cls, string):
        brand, model, price = string.split(',')
        return cls(brand, model, price)
    
    def full_brand(self):
        return f"The name of the brand is {self.bn} {self.mn}"
    
    @staticmethod
    def intro():
        print("Welcome to Laptop Shop")

L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)
L4 = Laptop.from_string('HP,MNOP,70000')

Laptop.intro()
print(L4.full_brand())

Class method as constructor in Python

class Laptop():
    discount = 10
    def __init__(self, brand_name, model_name, price):
        #initializing instance varaibles
        self.bn = brand_name
        self.mn = model_name
        self.p = price

    def apply_disc(self):
        after_disc = self.p - (self.p * (Laptop.discount/100))
        return after_disc

    def apply_special_disc(self, disc):
        after_spcl_disc = self.p - (self.p * (disc/100))
        return after_spcl_disc
    @classmethod
    def from_string(cls, string):
        brand, model, price = string.split(',')
        return cls(brand, model, price)
    
    def full_brand(self):
        return f"The name of the brand is {self.bn} {self.mn}"

L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)
L4 = Laptop.from_string('HP,MNOP,70000')

print(L4.full_brand())

Implementing class method in Python

class Laptop():
    count_instance = 0
    def __init__(self, brand_name, model_name, price):
        #initializing instance varaibles
        self.bn = brand_name
        self.mn = model_name
        self.p = price
        Laptop.count_instance+=1 #counting the number of instances

    def apply_disc(self, disc):
        after_disc = self.p - (self.p * (disc/100))
        return after_disc
    
    @classmethod
    def countInstance(cls):
        return f"You have called {cls.count_instance} instances of class {cls.__name__}"

L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)

print(L1.apply_disc(20))
print(Laptop.countInstance())

Counting number of instances of a class in Python

class Laptop():
    count_instance = 0
    def __init__(self, brand_name, model_name, price):
        #initializing instance varaibles
        self.bn = brand_name
        self.mn = model_name
        self.p = price
        Laptop.count_instance+=1 #counting the number of instances

    def apply_disc(self, disc):
        after_disc = self.p - (self.p * (disc/100))
        return after_disc

L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)

print(L1.apply_disc(20))
print(f"Number of instances called = {Laptop.count_instance}")

Using instance variable and class variable wisely in Python

class Laptop():
    discount = 10
    def __init__(self, brand_name, model_name, price):
        #initializing instance varaibles
        self.bn = brand_name
        self.mn = model_name
        self.p = price

    def apply_disc(self):
        after_disc = self.p - (self.p * (Laptop.discount/100))
        return after_disc

    def apply_special_disc(self, disc):
        after_spcl_disc = self.p - (self.p * (disc/100))
        return after_spcl_disc

L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)

print(L1.apply_disc())
print(L1.apply_special_disc(45))

Basic Class and Object implementation using Python

  1. The init() method gets called depends upon how many objects we create of that class.
  2. init() method always starts with self keyword according to naming convention. It is not always necessary to write self.
  3. init() is a method as well as a constructor in Python.
class Person:
    def __init__(self, first_name, last_name, age):
        print("Init method / constructor gets called here.")
        self.fn = first_name
        self.ln = last_name
        self.a = age

p1 = Person('Sayan', 'Dey', 21)
p2 = Person('Dey', 'Sayan', 21)
print(p1.fn)
print(p2.ln)