|
| 1 | +"""OOP in python |
| 2 | +To map with real world scenarios, we started using objects in code. |
| 3 | +This is called object oriented programming |
| 4 | +Procedral - seq code |
| 5 | +function - redundancy < , reusablity > |
| 6 | +oop - we can also: redundancy < , reusablity > |
| 7 | +
|
| 8 | +class :1st we create class [ class is bluePrint for creathing objects ] |
| 9 | +object :""" |
| 10 | + |
| 11 | +#creating class |
| 12 | +class Student: |
| 13 | + name = "Sakshi" |
| 14 | + |
| 15 | +s1 = Student() |
| 16 | +print(s1) #op - <__main__.Student object at 0x0000014C58275850> |
| 17 | +print(s1.name) #Sakshi |
| 18 | + |
| 19 | +s2 = Student() |
| 20 | +print(s2.name) #Sakshi |
| 21 | + |
| 22 | +class Car: |
| 23 | + color = "pink" |
| 24 | + brand = "BMW" |
| 25 | + |
| 26 | +c1 = Car() |
| 27 | +print(c1.color) |
| 28 | +print(c1.brand) |
| 29 | + |
| 30 | +""" |
| 31 | +__init__ Function |
| 32 | +constructor is called in every new obj |
| 33 | +All class have a function called __init__(),which is always executed |
| 34 | +when the object is being initiated""" |
| 35 | + |
| 36 | +class Student2: |
| 37 | + college_name ="jain" #when we know the value is comman , class attr |
| 38 | + name = "anonymous" #class attr , when we have same name then obj attr is printed |
| 39 | + #default constructor |
| 40 | + def __init__(self): |
| 41 | + pass |
| 42 | + #parameterzied constructor |
| 43 | + def __init__(self,name,marks): #we have to pass the self arrg |
| 44 | + print("Adding new student database") |
| 45 | + self.name = name |
| 46 | + self.marks = marks |
| 47 | + |
| 48 | + |
| 49 | +#s3 = Student2() #<__main__.Student object at 0x000001C7E1C8B7A0> |
| 50 | +#print(s3) #<__main__.Student object at 0x000001C7E1C8B7A0> |
| 51 | +s3 = Student2("sakshi",89) #data is called attibutes |
| 52 | +print(s3.name,s3.marks) |
| 53 | + |
| 54 | +s4 = Student2("Nida",89) |
| 55 | +print(s4.name,s4.marks) |
| 56 | + |
| 57 | +print(s4.college_name) #jain |
| 58 | +print(Student2.college_name) #jain |
| 59 | + |
| 60 | +""" |
| 61 | +Methods |
| 62 | +class is colletion of data(attr) and methods |
| 63 | +Method are function that belongs to objects""" |
| 64 | + |
| 65 | +#ex for method |
| 66 | +class Student3: |
| 67 | + college1_name ="jain" |
| 68 | + |
| 69 | + #parameterzied constructor |
| 70 | + def __init__(self,name1,marks1): #we have to pass the self arrg self.name1 = name1 |
| 71 | + self.name1 = name1 |
| 72 | + self.marks1 = marks1 |
| 73 | + |
| 74 | + def wlcm(self): |
| 75 | + print("wlcm student",self.name1) |
| 76 | + |
| 77 | + def get_marks(self): |
| 78 | + return self.marks1 |
| 79 | + |
| 80 | + |
| 81 | +s7 = Student3("sak",99) |
| 82 | +print(s7.name1,s7.marks1) |
| 83 | +s7.wlcm() #calling the method |
| 84 | +print(Student3.get_marks1()) |
| 85 | + |
| 86 | +#crete student class that takes name and marks of 3 sub as aruguments |
| 87 | +#in constructor then create a mentod to print the average |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +class Std: |
| 92 | + def __init__(self,name9,marks7): |
| 93 | + self.name9 = name9 |
| 94 | + self.marks7 = marks7 |
| 95 | + |
| 96 | + def avg(self): |
| 97 | + sum = 0 |
| 98 | + for val in self.marks7: |
| 99 | + sum += val |
| 100 | + print("hi",self.name9,sum/3) |
| 101 | + |
| 102 | +s5 =Std("sakshi",[99,98,97]) |
| 103 | +s5.avg() #hi sakshi 98.0 |
| 104 | + |
| 105 | +s5.name2 ="Sak" |
| 106 | +s5.avg() #hi Sak 98.0 |
| 107 | + |
| 108 | + |
| 109 | +""" |
| 110 | +Static Methods |
| 111 | +methods that dont use the self parameter(work at class level) |
| 112 | +@staticmethod -> convert a function to be static methods |
| 113 | +*when to use > This is comman for all class(obj) |
| 114 | + > whn there is no use of class attr or instances attr |
| 115 | + EX: |
| 116 | + @staticmethod |
| 117 | + def start(): |
| 118 | + print("str") |
| 119 | +""" |
| 120 | + |
| 121 | +class Hello: |
| 122 | + @staticmethod #decorator |
| 123 | + def hello(): |
| 124 | + print("hello") |
| 125 | + |
| 126 | + |
| 127 | +s0 =Hello() |
| 128 | +s0.hello() |
0 commit comments