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.
  • 相关阅读:
    ArtTmeplate模板+取结接口
    取接口
    ionic 基本布局
    angular通过路由实现跳转 resource加载数据
    总结
    JSON和JSONP
    js中sort()方法的用法,参数以及排序原理
    计算机语言的发展史
    Jquery+PHP实现简单的前后台数据交互实现注册登录,添加留言功能
    Jquery回调函数应用实例解析
  • 原文地址:https://www.cnblogs.com/eilinge/p/9705367.html
Copyright © 2011-2022 走看看