zoukankan      html  css  js  c++  java
  • Python Frame

    http://farmdev.com/src/secrets/framehack/index.html

    sys._getframe([depth])

    Return a frame object from the call stack
    depth 表示调用栈的深度,默认为0,返回栈顶 

    def get_cur_info():
        print sys._getframe().f_code.co_filename #当前文件名,可以通过__file__获得
        print sys._getframe(0).f_code.co_name   #当前函数名
        print sys._getframe(1).f_code.co_name
        #调用该函数的函数的名字,如果没有被调用,则返回<module>,貌似call stack的栈低
        print sys._getframe().f_lineno #当前行号

     例:

     1 import sys
     2 
     3 def one():
     4     two()
     5 
     6 def two():
     7     three()
     8 
     9 def three():
    10     for num in range(3):
    11         frame = sys._getframe(num)
    12         show_frame(num, frame)
    13 
    14 def show_frame(num, frame):
    15     print frame
    16     print "  frame     = sys._getframe(%s)" % num
    17     print "  function  = %s()" % frame.f_code.co_name
    18     print "  file/line = %s:%s" % (frame.f_code.co_filename, frame.f_lineno)
    19 
    20 one()

    输出结果:

    <frame object at 0x606c50>
      frame     = sys._getframe(0)
      function  = three()
      file/line = stack.py:12
    <frame object at 0x180be10>
      frame     = sys._getframe(1)
      function  = two()
      file/line = stack.py:7
    <frame object at 0x608d30>
      frame     = sys._getframe(2)
      function  = one()
      file/line = stack.py:4

    sys._getframe([depth]) -> frameobject

    frameobject 属性

    f_back -> frameobject

    f_builtins -> dict key:python的内置函数名

    f_code -> 代码对象

  • 相关阅读:
    类的嵌套
    一种设计模式--单例模式
    python中的类(二)
    Python中的类(一)
    基于session和cookie的登录验证(CBV模式)
    Django中的CBV和FBV
    python3 装饰器
    cookie和session
    基于cookie和session的登录验证
    python3 安装win32api
  • 原文地址:https://www.cnblogs.com/xiyuan2016/p/10339434.html
Copyright © 2011-2022 走看看