-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path内置方法二.py
96 lines (75 loc) · 2.1 KB
/
内置方法二.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
d = dict({'name':'egon'})
print(isinstance(d,dict))
print(d)
中国 = "China"
print(中国)
class Foo:
def __init__(self,name):
self.name = name
def __getitem__(self, item):
return self.__dict__.get(item)
def __setitem__(self, key, value):
self.__dict__[key] = value
def __delitem__(self, key):
del self.__dict__[key]
# 查看属性
obj = Foo('egon')
print(obj['name'])
# 设置属性
obj['sex'] = 'male'
print(obj.sex)
# 删除属性
del obj['name']
print(obj.__dict__)
# 改变字符串的显示 __str__,__repr__
# 自定制格式化字符串__format__
d = dict({'name':'egon'})
print(isinstance(d,dict)) # d 是 dict类的实例
print(d)
class Foo:
pass
obj = Foo()
print(obj)
# __str__方法定义后,会在打印对象的收,把字符串的的结果作为打印的结果
class People:
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self): # 必须返回一个字符串
return '<name:%s,age:%s>' % (self.name,self.age)
obj = People('egon',18)
print(obj)
format_dict={
'nat':'{obj.name}-{obj.addr}-{obj.type}',# 学校名-地址-类型
'tna':'{obj.type}:{obj.name}:{obj.addr}',# 类型:名:地址
'tan':'{obj.type}/{obj.addr}/{obj.name}',# 类型/地址/名
}
class School:
def __init__(self,name,addr,type):
self.name = name
self.addr = addr
self.type = type
def __repr__(self):
return 'School(%s,%s)' %(self.name,self.addr)
def __str__(self):
return '(%s,%s)' %(self.name,self.addr)
def __format__(self, format_spec):
if not format_spec or format_spec not in format_dict:
format_spec='nat'
fmt = format_dict[format_spec]
return fmt.format(obj=self)
s1 = School('oldboy1','北京','私立')
print('from repr:',repr(s1))
print('from str:',str(s1))
print(s1)
print(format(s1,'nat'))
print(format(s1,'tna'))
print(format(s1,'asfdasdffd'))
# issubclass和isinstance
class A:
pass
class B(A):
pass
print(issubclass(B,A)) #B是A的子类,返回True
a1=A()
print(isinstance(a1,A)) #a1是A的实例