python inspect模块, getmembers方法使用

先创建 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是一个非常好用的方法.常用于获取文件中的各种对象,而这个方法的第二个参数就是过滤的条件

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值