-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOop_Basic.py
121 lines (87 loc) · 2.8 KB
/
Oop_Basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Object-Oriented Programming Concepts
There are two-way of writing code
1- Functional Programming
2- OOP
Note: Python Build-in Functionalities are develop using OOP.
Total 6 Concepts in terms of OOP
Class
Objects
Polymorphism
Encapsulation
Inheritance
Data Abstraction
Note: Very important programming Concepts, give max time to work on it.
Important Points:
1- One class have many instances called Objects.
2- Attributes|Property, Behavior, Methods.
3- Attributes define by variables and behavior by methods.
4- Self is used to represent an object in a method.
5- __Init__ is a method for defining Attribute for objects, it's like constructor.
Note: A class is a collection of objects.
"""
def python_build_in():
a = 10
print(type(a)) # Shows variable 'a' of class <int>
def basic_class_concept():
class Student:
# Class Attribute
university = 'COMSATS, Lahore Campus'
# Creating an Object
hassan = Student()
print(hassan.university) # Not prefer way.
print(hassan.__class__.university) # clarify more (prefer).
def class_concept_instance_attr():
class Student:
# declaring instance Attr using Constructor
def __init__(self, reg_no):
self.reg_no = reg_no
# Instance Method
def display(self):
return self.reg_no
# Class Attribute
university = 'COMSATS, Lahore Campus'
# Creating an Object
# pass instance attribute value.
hassan = Student('SP20-BCS-114')
print(hassan.__class__.university)
# Instance Attr
print(hassan.reg_no)
print(hassan.display())
print(Student.display(hassan)) # Work Same.
print(type(hassan))
def class_advance_concept():
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def compare(self, other):
return True if self.age == other.age else False
def update(self):
self.age = 22
p1 = Person('Hassan', 21)
p2 = Person('Hamid', 20)
print('Both are same') if p1.compare(p2) else print('Not same')
p2.update()
print(p1.name + "\n" + p2.name)
print(f"{p2.age}")
# Variables Type in OOP (instance or (static or class)
def variables_oop():
class Car:
def __init__(self):
self.Company = 'Toyota'
self.model = 'Corolla'
# Class variable.
seater = 5
# Method of display
def display(self):
print(self.Company + '\n' + self.model + '\n' + self.seater + ' ')
return ''
# you can change variables inside class using object.
car1 = Car()
car1.Company = 'Honda'
car1.model = 'BRV'
car1.seater = '7'
print(car1.display())
if __name__ == '__main__':
variables_oop()