-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionIntrospection.py
executable file
·152 lines (117 loc) · 3.47 KB
/
FunctionIntrospection.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 25 21:42:57 2021
@author: maherme
"""
#%%
def my_func(a: "mandatory positional",
b: "optional positional" =1,
c=2,
*args: "add extra positional here",
kw1,
kw2=100,
kw3=200,
**kwargs: "provide extra kw-only here") -> "dones nothing":
"""This function does nothing but does have various parameters
and annotations.
"""
i = 10
j = 20
print(my_func.__doc__)
print("-----------------------------------------")
print(my_func.__annotations__)
print("-----------------------------------------")
my_func.short_description = "this is a function that does nothing much"
print(my_func.short_description)
#%%
dir(my_func) # Run this in the command line and this shows all the attributes.
#%%
print(my_func.__name__)
a = id(my_func)
print(a)
#%%
def func_call(f):
print(id(f))
print(f.__name__)
func_call(my_func)
#%%
print(my_func.__defaults__) # This shows the default parameters
print(my_func.__kwdefaults__) # This shows the default keyword parameters
#%%
print(my_func.__code__)
print("-----------------------------------------")
dir(my_func.__code__) # code is an object so we can watch its attributes
print(my_func.__code__.co_name)
print("-----------------------------------------")
print(my_func.__code__.co_varnames)
print("-----------------------------------------")
print(my_func.__code__.co_argcount) # This shows only positional arguments
#%%
# It is easier use the inspect module:
import inspect
from inspect import isfunction, ismethod, isroutine
a = 10
print(isfunction(a))
print(isfunction(my_func))
print(ismethod(my_func))
#%%
class MyClass:
def f(self):
pass
print(isfunction(MyClass.f))
my_obj = MyClass()
print(isfunction(my_obj.f))
print(ismethod(my_obj.f))
print(isroutine(my_obj.f))
print(isroutine(MyClass.f))
#%%
# TODO: Fix this function
# currently does nothing, but should do ...
def my_func(a: "mandatory positional",
b: "optional positional" =1,
c=2,
*args: "add extra positional here",
kw1,
kw2=100,
kw3=200,
**kwargs: "provide extra kw-only here") -> "dones nothing":
"""This function does nothing but does have various parameters
and annotations.
"""
i = 10
j = 20
a = i + j
return a
print(inspect.getsource(my_func))
print("-----------------------------------------")
print(inspect.getmodule(my_func))
print("-----------------------------------------")
print(inspect.getcomments(my_func))
#%%
print(inspect.signature(my_func))
print("-----------------------------------------")
dir(inspect.signature(my_func)) # Run in the command line
print("-----------------------------------------")
print(my_func.__annotations__)
print("-----------------------------------------")
print(inspect.signature(my_func).return_annotation)
#%%
sig = inspect.signature(my_func)
print(sig.parameters)
#%%
for k, v in sig.parameters.items():
print(dir(v))
#%%
for k, param in sig.parameters.items():
print('Key:', k)
print('Name:', param.name)
print('Default:', param.default)
print('Annotation:', param.annotation)
print('Kind:', param.kind)
print("-----------------------------------------")
#%%
help(divmod) # Notice the / character, it means the parameters are positional only:
for param in inspect.signature(divmod).parameters.values():
print(param.kind)
#%%