先创建 t1.py
# t1.py
from imp import new_module
from types import CodeType
import __builtin__
class C_1:
pass
class D_1:
pass
class E_1:
def do_sth(self):
return "hello"
def test_1():
pass
def test_2():
yield 1
b = new_module("__conf__")
c = E_1.do_sth
执行 t2.py
# t2.py
import inspect
t2 = __import__("t2")
result = inspect.getmembers(t2,inspect.isclass)
result2 = inspect.getmembers(t2,inspect.isfunction)
result3 = inspect.getmembers(t2,inspect.ismodule)
result4 = inspect.getmembers(t2,inspect.ismethod)
result5 = inspect.getmembers(t2,inspect.isgeneratorfunction)
result6 = inspect.getmembers(t2,inspect.isbuiltin)
print result
print result2
print result3
print result4
print result5
print result6
getmembers是一个非常好用的方法.常用于获取文件中的各种对象,而这个方法的第二个参数就是过滤的条件