Over all class concept in python
class variable
class Animals:
alive = True # class variable
eat = "Yes they can eat" # class variable
How to used them
Umair = Animals()
print(Umair.eat)
=========================================================================================
Instant variable
class eat():
def __init__(self, pizza , limit):
self.pizza = pizza
self.limit = limit
def food(self):
return f"He likes {self.limit} {self.pizza}"
# if we have numbers then we can also perform this type of functions on it
# def add(self):
# return int(self.pizza)*int(self.limit)
Umair = eat("Pizza", "4")
print(Umair.food())
print(Umair.pizza)
print(Umair.add())
Inherit
class Animals: # parant class
Alive = True
def eat(self):
return "Yes obviously they can eat other wise they all are dead."
def sleep(self):
return "Yes they all are sleeping in the nights"
class fish(Animals): # child class
def swim(self):
return "Yes a fish have ability to swiw"
class Hawk(Animals): # child class
def fly(self):
return "Yes a hawk have ability to killed a eagle."
class rabit(Animals): # child class
def run(self):
return "Yes a rabit run fast even no one come close to him."
print(Animals.eat("YES"))
print(rabit.sleep("k"))
print(Hawk.fly("d"))
print(rabit.run("f"))
=============================================================================================
Mulitlevel Inheritence
in mulitleve inheritence a class have a parant class and the parant class have also a parant class
class Organism:
alive = True
class Animal(Organism):
def eat(self):
return "this animal is eating"
class dog(Animal):
def bark(self):
return "This animal is still barking"
dog = dog()
print(dog.eat())
print(dog.alive)
================================================================================
Multiple Inheritence : when child class is derived from more then one parant class:
class prey:
def flee(self):
print("This animaly flees")
class predator:
def hunt(self):
print("this animal is hunting")
class Rabbit(prey):
pass
class Hawk(predator):
pass
class fish(prey, predator):
pass
rabbit = Rabbit()
hawk = Hawk()
fish = fish()
fish.hunt()
fish.flee()
hawk.hunt()
***************************************************************************************
Over Riding method
class Animal:
def eat(self):
print("this animal is eating")
class Rabbit(Animal):
def eat(self):
print("This rabbit is eating carrot")
rabbit = Rabbit()
rabbit.eat()
****************************************************************************************
Method chaning = calling multiple mehtods sequentially
each call performs an action on the same object and returns self
class Car:
def turn_on(self):
print("You start the engine")
return self
def drive(self):
print("You Drive the car")
return self
def brake(self):
print("You step on brake")
return self
def turn_off(self):
print("You turn off the car")
return self
car = Car()
# simple methods
# print("This is the old method")
# car.turn_on()
# car.drive()
# # Chaining mathod
# print("This is the new method")
# car.turn_on().drive()
# car.brake().turn_off()
# both line print the same output but the last one increase the read ability
car.turn_on().drive().brake().turn_off()
car.turn_on()\
.drive()\
.brake()\
.turn_off()
***************************************************************************
Super() Function used to give accese to the methods of a parent calss.
Returns a temporary object of a parent class when used
class sum:
def __init__(self, first_word, second_word):
self.first_word = first_word
self.second_word = second_word
def add(self):
return self.first_word+self.second_word
class mul(sum):
def __init__(self, first_word, second_word):
super().__init__(first_word,second_word)
def mult(self):
return self.first_word*self.second_word
One = sum(1 , 4)
print(One.add())
second = mul(3,4)
print(second.mult())
*****************************************************************************
Prevent s a user from creating an objects of that class
+ compels a user to override abstract methods in a child class
Abstract classes = a class which contains one or more abstract mehtods
Abstract methods = a methed that has declaration but does not have an implementation
from abc import ABC, abstractclassmethod
class vehicle(ABC):
@abstractclassmethod
def start(self):
pass
class motercycle(vehicle):
def go(self):
print("Its time to go through the function")
def start(self):
print("Please start the bike first")
class car(vehicle):
def go(self):
print("You are ready to go ")
def start(self):
print("You must have to start you vehical before go")
bike = motercycle()
bike.go()
bike.start()
car = car()
car.start()
car.go()
********************************************************************************
Pass objects as arguments in the functions
class vehical:
color = None
def change_color(vehical,color):
vehical.color = color
car_1= vehical()
change_color(car_1,"green")
print(car_1.color)
class wash:
wash = None
def wash_cars(car,wash):
car.wash = wash
bike_1 = wash()
wash_cars(bike_1,"Wash")
print(bike_1.wash)
=======================================================================
class duck:
def walk(self):
print("This duck is walk")
def talk(self):
print("This duck is talk")
class chicken:
def walk(self):
print("This chicken is walk")
def talk(sefl):
print("This chicken is talk")
class person:
def catch(self, duck):
duck.walk()
duck.talk()
print("You have caught the chicken")
chicken = chicken()
duck = duck()
person = person()
person.catch(chicken)
person.catch(duck)
=================================================================
Walrus operators (:=)
print(happy := "hell")
foods = list()
while True:
food = input("What food do yo like?: ")
if food == "quit":
break
foods.append(food)
for ch in foods:
print(f"{ch}, ",end="")
Second method
foods = list()
while food := input("what food do you like?=") !="quit":
foods.append(food)
==============================================================
assigning a function to a variable
def hello():
print("hello bro")
hi = hello
hi()
hello()
say = print
say("Whoa! I can't believe this work! ")
================================================================
Higher order Function in python
accept function as an arguments
def loud(text):
return text.upper()
def quiet(text):
return text.lower()
def isprint(func):
text = func("hello")
print(text)
isprint(loud)
isprint(quiet)
return a functio as return
def div(x):
def divide(y):
return y/x
return divide
Divide = div(3)
print(Divide(30))
============================================================
Lambda function
Normal form
def double(x):
return x*x
print(double(3))
By using lambda function
double = lambda x: x*x
print(double(3))
mul = lambda x,y:x-y
print(mul(7,5))
full_name = lambda first_name, last_name:f"{first_name} {last_name}"
print(full_name("Umair", "jutt"))
age_check = lambda age:"Your are age is greater then 18" if age>18 else " you age is less then 18"
print(age_check(30))
=======================================================================================
Comments
Post a Comment