Repository navigation
Expand file tree
/
Copy pathIntrospection.py
More file actions
68 lines (50 loc) · 2.23 KB
/
Copy pathIntrospection.py
File metadata and controls
68 lines (50 loc) · 2.23 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
# Домашнее задание по теме "Интроспекция"
from pprint import pprint
import inspect # Это возьмём из лекции "Лекция. Интроспекция. 1.2"
class MyClass:
def __init__(self):
self.attr_01 = 27
self.attr_02 = 'string'
def Fibonacci(self, n):
if n < 0:
return 0
elif n == 0:
return 0
elif n == 1:
return 1
else:
return self.Fibonacci(n - 1) + self.Fibonacci(n - 2)
def method2(self):
print("this is method2")
class A(MyClass):
pass
def introspection_info(obj):
_list = dir(obj)
result = {'тип объекта': type(obj),
'атрибуты объекта': [attr for attr in _list if not callable(getattr(obj, attr))],
'методы объекта': [method for method in _list if callable(getattr(obj, method))],
'принадлежность к модулю': obj.__module__ if hasattr(obj, '__module__') else type(obj)}
return result
def shorted_introspection_info(obj):
_list = dir(obj)
result = {'тип объекта': type(obj),
'атрибуты объекта': [name for name, value in inspect.getmembers(obj) if not callable(value) and not name.startswith("__")],
'методы объекта': [method for method in _list if callable(getattr(obj, method))],
'принадлежность к модулю': inspect.getmodule(obj),
'сигнатура метода': inspect.signature(obj.__init__),
'Является ли модулем': inspect.ismodule(obj),
'Получаем строку документации': inspect.getdoc(obj)
}
return result
mcl = MyClass()
mcl.Fibonacci(12)
aa = A()
number_info = introspection_info(42)
custom_info = introspection_info(mcl)
pprint(number_info)
pprint(custom_info)
print('----------------------------------------------------------------------------------------------')
num_info = shorted_introspection_info(55)
cus_info = shorted_introspection_info(aa)
pprint(num_info)
pprint(cus_info)