forked from yidao620c/python3-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassinstance.py
More file actions
41 lines (28 loc) · 813 Bytes
/
classinstance.py
File metadata and controls
41 lines (28 loc) · 813 Bytes
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: class和instance的练习
Desc :
"""
from types import MethodType
class Dog:
# 可变对象最好不要定义为类变量,防止共享时修改混乱
kind = 'canine' # class variable shared by all instances
def __init__(self, name):
self.name = name # instance variable unique to each instance
def change_dog():
Dog.kind = 'another'
def set_age(self, age):
print('set age...')
self.age = age
if __name__ == '__main__':
a = Dog('adog')
b = Dog('bdog')
print(Dog.kind, a.kind, a.name)
print(Dog.kind, b.kind, b.name)
change_dog()
print(Dog.kind, a.kind, a.name)
print(Dog.kind, b.kind, b.name)
Dog.set_age = MethodType(set_age, Dog)
# b = Dog('bdog')
b.set_age(111)