Uh oh!
There was an error while loading. Please reload this page.
forked from SergioJune/python_test
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest19.py
More file actions
Latest commit
85 lines (63 loc) · 1.8 KB
/
Copy pathtest19.py
File metadata and controls
85 lines (63 loc) · 1.8 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'练习面向对象的获取对象的属性和信息'
__author__='sergiojune'
importtypes
# 判断基本类型
print(type('str'))
print(type(12))
print(type(12.36))
deffn():
print('fn----')
# 函数类型
print(type(fn))
print(type('d') ==str)
print(type(56) ==int)
print(type(fn) ==types.FunctionType)
classAnimal(object):
defrun(self):
print('run ------')
animal=Animal()
print(type(animal))
# 内置函数类型
print(type(abs))
# 还可以使用isinstance函数来进行判断类型
print(isinstance(123, int))
print(isinstance('dd', str))
# 判断对象类型
print(isinstance([1, 2], list))
print(isinstance({1: 2}, dict))
print(isinstance(fn, types.FunctionType))
print(isinstance(abs, types.BuiltinFunctionType))
print(isinstance(animal, Animal))
# 第二个参数可以写一个元组。里面的关系表示或
print(isinstance([2, 3, 5], (list, tuple)))
# 对对象的属性进行操作
classMyDog(Animal):
def__init__(self):
self.x=9
# 这个方法就是调用len函数时被调用的方法
def__len__(self):
return100
defpower(self):
returnself.x*self.x
dog=MyDog()
print(len(dog))
# 获取实例的属性列表
print(dir(dog))
# 获取实例的属性
print(getattr(dog, 'x'))
# 可以设置默认值,当没有时就会返回默认值
print(getattr(dog, 'high', 404))
# 设置属性
setattr(dog, 'high', 0.86)
# 获取上述设置的属性
print(getattr(dog, 'high', 404))
# 还可以判断是否存在该属性
print(hasattr(dog ,'x'))
print(hasattr(dog, 'attr'))
# 还可以获得实例的方法
print(getattr(dog, 'power'))
p=getattr(dog, 'power')
# 下面这个p相当于 dog.power
print(p())