-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP.py
More file actions
146 lines (90 loc) · 2.58 KB
/
OOP.py
File metadata and controls
146 lines (90 loc) · 2.58 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
OOP
* object oriented programing is a way to use python to create our own objects.
* it can be a point of great confusion for beginners, mainly because often it is taught poorly
* let's try our best to save you from any confusion by systimaticlly showing you the thought
process behind OOP and why we would need it.
* we will use it quite a bit for django, so let's get started
"""
# everything in python is class
print(type(12))
print(type(12.3))
print(type("scscds"))
print(type([1,2,3,5]))
print('---------------')
class MyClass():
pass
x = MyClass()
print(x)
print('---------------')
# The self Parameter
# * The self parameter is a reference to the current instance of the class,
# and is used to access variables that belongs to the class.
# * It does not have to be named self , you can call it whatever you like,
# but it has to be the first parameter of any function in the class
# __init__ isconstructor
class Dog():
# class object attribute
specise = 'mammal'
def __init__(self , breed, name):
self.name = name
self.breed = breed
mydog = Dog('Lab', 'Sammy')
print(mydog.breed)
print(mydog.name)
print(mydog.specise)
print('---------------')
class Circle():
pi = 3.14
def __init__(self, reduce) -> None:
self.reduce = reduce
def area(self):
return self.reduce * self.reduce * Circle.pi
def set_reduce(self, new_r):
self.pi = new_r
myc = Circle(4)
myc.set_reduce(99)
print(myc.area())
print('---------------')
class Animal():
def __init__(self) -> None:
print('Animal created')
def whoAmI(self):
print('Animal')
def eat(self):
print('Eating')
myanimal = Animal()
myanimal.whoAmI()
myanimal.eat()
print('---------------')
# inheritance
class Dog(Animal):
def __init__(self) -> None:
# Animal.__init__(self)
print("Dog created")
def bark(self):
print('woof')
# method over writhing
def eat(self):
print("Dog eating")
mydog = Dog()
mydog.whoAmI()
mydog.eat()
mydog.bark()
print('---------------')
# special methods
class Book():
def __init__(self, title, author, pages) -> None:
self.title = title
self.author = author
self.pages = pages
def __str__(self) -> str:
return 'title: {}, author: {}, pajes: {}'.format(self.title, self.author, self.pages)
def __len__(self):
return len(self.author)
def __del__(self):
print('a book is destroyed!')
book = Book('python', 'ruhollah', 100)
print(book)
print(len(book))
del book