• 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.
  • 相关阅读:
    设计模式----工厂模式
    设计模式----简单工厂
    log4net使用详解
    link/Extended dependency 无法显示连接
    Abp框架之执行Update-Database 命令系列错误
    Index API
    使用Java客户端操作elasticsearch(二)
    elasticsearch之分词插件使用
    使用Java Low Level REST Client操作elasticsearch
    js如何获取隐藏的元素的高度
  • 原文地址:https://www.cnblogs.com/eilinge/p/9705367.html
Copyright © 2020-2023  润新知