-
Notifications
You must be signed in to change notification settings - Fork 2
/
introspection.py
60 lines (48 loc) · 1.25 KB
/
introspection.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
#!/usr/bin/python2.7
## functions
def foo():
"""hello world from docstring"""
x = 1
return lambda: x
print "function foo dir", foo.__doc__
f = foo()
print "function f dir", dir(f)
print "f closure", f.__closure__
## modules
print "sys module dir:", dir(sys)
## classes
class A():
def __init__(self):
self.x = 1
def foo(self):
self.y = 2
print "class A dir:", dir(A)
a = A()
print "instance of class A dir:", dir(a)
a.foo()
print "instance of class A dir after foo:", dir(a)
## if the object provides __dir__ method then it is used to return
## list of attributes
class C(object): # you *MUST* inherit object for this to work
# it seems that usually you should inherit object
def __dir__(self):
return ["hello", "from", "my", "__dir__"]
print "class C dir", dir(C) # will work as usuall
c = C()
print "instance of class C dir", dir(c) # returns sorted(["hello", "from", "my", "__dir__"])
## get list of methods
print "Methods in A"
for m in dir(A):
if callable(getattr(A, m)):
print m
print "Methods in instance of A"
aa = A()
for m in dir(aa):
if callable(getattr(aa, m)):
print m
## using getattr
class X(object):
def foo(self):
return 1
x = X()
assert getattr(x, "foo")() == 1