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.
  • 相关阅读:
    左滑删除
    关于ajax里边不能识别$(this)的解决方法
    前端面试常见问答
    推荐10 个短小却超实用的 JavaScript 代码段
    jquery实现滚动到页面底部时无限加载内容的代码
    理解MVC,MVP和MVVM设计模式
    JS toLowerCase()方法 toUpperCase()方法
    前端知识体系
    JavaScript易错知识点整理
    HttpUrlConnection Post请求
  • 原文地址:https://www.cnblogs.com/eilinge/p/9705367.html
Copyright © 2011-2022 走看看