zoukankan      html  css  js  c++  java
  • python--inspect

      给大家介绍下用的比较少的模块--Inspect

    inspect模块用于收集python对象的信息,可以获取类或函数的参数的信息,源码,解析堆栈,对对象进行类型检查等等

    import hello
    import inspect
    
    print(inspect.getsource(hello))
    
    def h():
        print 'hello'
    
    def hm(m,k):
        print m,k
    
    class w(object):
        def __init__(a,self):
            name = a
        def g(self):
            print name,'hello world'

    特定的方法调用

    #返回对象的源代码
    >>> inspect.getsource(hello.w)
    "class w(object):
        def __init__(a,self):
    "
    "name = a
        def g(self):
            print name,'hello world'
    "
    
    #方便的获取__init__的**参数
    >>> inspect.getargspec(hello.w.__init__)
    ArgSpec(args=['a', 'self'], varargs=None, keywords=None, defaults=None)

    可以通过help()方法获取该使用函数的文本

    >>> help(inspect.getsourcefile)
    Help on function getsourcefile in module inspect:
    
    getsourcefile(object)
        Return the filename that can be used to locate an object's source.
        Return None if no way can be identified to get the source.
        
    >>> help(inspect.getmembers)
    Help on function getmembers in module inspect:
    
    getmembers(object, predicate=None)
        Return all members of an object as (name, value) pairs sorted by name.
        Optionally, only return members that satisfy a given predicate.
    
    >>> help(inspect.stack)
    Help on function stack in module inspect:
    
    stack(context=1)
        Return a list of records for the stack above the caller's frame.
  • 相关阅读:
    将jetty嵌入到应用中的简单案例
    修改jetty的默认端口号
    Linux下安装jetty服务器
    影视-纪录片:《超级装备》
    影视-纪录片:《飞向月球》
    影视-电视剧:《罪域》
    影视-电视剧:《黎明之前》
    影视-电视剧:《人民的名义》
    几何:不可能图形
    几何:悖理图形
  • 原文地址:https://www.cnblogs.com/eilinge/p/9705367.html
Copyright © 2011-2022 走看看