zoukankan      html  css  js  c++  java
  • python inspect.stack() 的简单使用

    1.

    #python
    # -*- encoding: utf-8 -*-
    #获取函数的名字
    import inspect
    def debug():
        callnamer = inspect.stack()
        print('[debug] enter: {}'.format(callnamer))
    
    debug()

    [debug] enter: [FrameInfo(frame=<frame object at 0x000000000096D448>, filename='E:/pythontest/sort.py', lineno=6, function='debug', code_context=[' callnamer = inspect.stack() '], index=0), FrameInfo(frame=<frame object at 0x00000000008F8828>, filename='E:/pythontest/sort.py', lineno=9, function='<module>', code_context=['debug() '], index=0)]

    可以看出是一个列表

    2.选取列表的第二项

    #python
    # -*- encoding: utf-8 -*-
    #获取函数的名字
    import inspect
    def debug():
        callnamer = inspect.stack()[1]
        print('[debug] enter: {}'.format(callnamer))
    
    debug()

    [debug] enter: FrameInfo(frame=<frame object at 0x00000000004A8828>, filename='E:/pythontest/sort.py', lineno=9, function='<module>', code_context=['debug() '], index=0)

    3.选取函数的名字

    #python
    # -*- encoding: utf-8 -*-
    #获取函数的名字
    import inspect
    def debug():
        callnamer = inspect.stack()[1][4]
        print('[debug] enter: {}'.format(callnamer))
    
    debug()

    [debug] enter: ['debug() ']

  • 相关阅读:
    python命令行参数处理
    linux进程管理
    hadoop
    linux进程间通信之信号
    HA for openstack
    ubutun 安装php7.1x
    php 函数小技巧(一)
    git error: RPC failed; result=56, HTTP code = 200
    php面试题汇总四(基础篇附答案)
    php面试题汇总三(基础篇附答案)
  • 原文地址:https://www.cnblogs.com/hixiaowei/p/10549731.html
Copyright © 2011-2022 走看看