forked from ls1248659692/python_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_and_instance.py
More file actions
376 lines (251 loc) · 6.48 KB
/
class_and_instance.py
File metadata and controls
376 lines (251 loc) · 6.48 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/python
# coding=utf8
__author__ = 'Jam'
__date__ = '2019/7/4 9:12'
print('*1*'.center(50, '-'))
class Animal(object):
def __init__(self, name, weight):
self.name = name
self.weight = weight
def print_weight(self):
print("%s : %s" % (self.name, self.weight))
dog = Animal('hasky', 90)
dog.print_weight()
print(dog.weight)
dog.weight = 100
print(dog.weight)
print('*2*'.center(50, '-'))
class Animal(object):
def __init__(self, name, weight):
self.__name = name
self.__weight = weight
def print_weight(self):
print("%s : %s" % (self.__name, self.__weight))
def get_name(self):
return self.__name
def get_weight(self):
return self.__weight
def set_name(self, name):
self.__name = name
def set_weight(self, weight):
self.__weight = weight
dog = Animal('hasky', 90)
print(dog.get_name())
dog.set_weight(100)
print(dog.get_weight())
print(dog.__dict__)
print('*3*'.center(50, '-'))
class Animal():
def __init__(self, name, weight):
self.name = name
self.weight = weight
def print_weight(self):
print("%s : %s" % (self.name, self.weight))
def run(self):
print('Animal is running')
class Dog(Animal):
def eat(self):
print('Dog is eating')
a = Dog("hasky", 90)
b = Animal('tt', 10)
print(isinstance(a, Dog))
print(isinstance(a, Animal))
print(isinstance(b, Animal))
print(isinstance(b, Dog))
print('*4*'.center(50, '-'))
def run_twice(obj):
obj.run()
a = Dog("hasky", 90)
b = Animal('tt', 10)
run_twice(a)
run_twice(b)
print('*5*'.center(50, '-'))
class Timer(object):
def run(self):
print('start....')
timer = Timer()
run_twice(timer)
print('*6*'.center(50, '-'))
print(dir('ABC'))
print('ABC'.__len__())
print(len('ABC'))
print('*7*'.center(50, '-'))
class Animal():
def __init__(self, name, weight):
self.name = name
self.weight = weight
def print_weight(self):
print("%s : %s" % (self.name, self.weight))
def run(self):
print('Animal is running')
dog = Animal('hasky', 20)
print(hasattr(dog, 'name'))
setattr(dog, 'height', 20)
print(getattr(dog, 'height'))
print(getattr(dog, 'type', 'default'))
print('*8*'.center(50, '-'))
func = getattr(dog, 'run')
print(func, func())
class Animal():
def __init__(self, name, weight):
self.name = name
self.weight = weight
dog = Animal('hasky', 90)
dog.height = 20
print(dog.height)
print('*9*'.center(50, '-'))
class Animal():
tt = 'ss'
def __init__(self, name, weight):
self.name = name
self.weight = weight
print(Animal.tt)
dog = Animal('hasky', 90)
print(dog.tt)
dog.tt = 'aa'
print(dog.tt)
print(Animal.tt)
print('*10*'.center(50, '-'))
class Animal():
pass
ani = Animal()
ani.name = 'hasky'
from types import MethodType
def set_age(self, age):
self.age = age
ani.set_age = MethodType(set_age, ani)
ani.set_age(20)
print(getattr(ani, 'age', 'default'))
print('*11*'.center(50, '-'))
class Animal(object):
__slots__ = ['name', 'weight']
ani = Animal()
ani.name = 'hasky'
ani.weight = 90
# print(setattr(ani, 'age', 'default'))
class Dog(Animal):
pass
dog = Dog()
dog.height = 20
print(dog.height)
print('*12*'.center(50, '-'))
class Animal(object):
def get_weight(self):
return self.weight
def set_weight(self, value):
if not isinstance(value, int):
raise ValueError('weight must be an integer!')
if not 0 <= value <= 200:
raise ValueError('weight must between 1 ~ 200!')
self.weight = value
ani = Animal()
ani.set_weight(60)
print(ani.get_weight())
ani.set_weight(200)
func = getattr(ani, 'set_weight')
print(func, func(1))
print(ani.set_weight(200))
print('*13*'.center(50, '-'))
class Animal(object):
@property
def birth(self):
return self._birth
@birth.setter
def birth(self, value):
if not isinstance(value, int):
raise ValueError('birth must be an integer!')
if not 1 <= value <= 2018:
raise ValueError('birth must between 1 ~ 2018!')
self._birth = value
@property
def age(self):
return 2018 - self._birth
ani = Animal()
ani.birth = 1958
print(ani.age)
print(ani.birth)
print('*14*'.center(50, '-'))
class Animal(object):
def __init__(self, name):
self.name = name
def __str__(self):
return 'Animal object with name : %s' % self.name
print(Animal('tt'))
a = Animal('tt')
print(a)
print('*15*'.center(50, '-'))
class Fib(object):
def __init__(self):
self.a, self.b = 0, 1
def __iter__(self):
print(self.a, self.b)
return self
def next(self):
self.a, self.b = self.b, self.a + self.b
if self.a > 10:
raise StopIteration()
return self.a
for n in Fib():
print(n)
print('*16*'.center(50, '-'))
class Fib(object):
def __getitem__(self, n):
if isinstance(n, int):
print(n)
a, b = 1, 1
for x in range(n):
a, b = b, a + b
return a
if isinstance(n, slice):
print(n)
start = n.start
stop = n.stop
print(start, stop, n.step)
if start is None:
start = 0
if stop is None:
print('Max number should be 10')
stop = 10 + 1
a, b = 1, 1
container = []
for x in range(stop):
if x >= start:
container.append(a)
a, b = b, a + b
return container
f = Fib()
print(range(0))
print(slice(1, 5))
print(f[0:11])
print(f[2:5])
print(f[:5])
print(f[1:])
print(f[10])
print('*17*'.center(50, '-'))
class Animal():
def __init__(self, name, weight):
self.name = name
self.weight = weight
def __getattr__(self, attr):
if attr == 'height':
return 20
a = Animal("hasky", 90)
print(a.height)
print('*18*'.center(50, '-'))
class Animal():
def __init__(self, name, weight):
self.name = name
self.weight = weight
def __getattr__(self, attr):
if attr == 'height':
return 178
elif attr == 'age':
return 18
elif attr == 'function':
return lambda: "function"
raise AttributeError('Animal object has no attribute %s' % attr)
a = Animal("hasky", 90)
print(a.height)
print(a.age)
print(a.function())
print('*19*'.center(50, '-'))