• __FUNCTION__ and __LINE__ in Python


    When you want to log something, C pre-processor macros __FUNCTION__ and __LINE__ are quite helpful (althogh the first is a GNU extension for GCC, AFAIK). Python have the __name__ pseudo-variable with the current namespace, but isn’t quite helpful when you want to find where things are going. Fortunately, you can “emulate” __FUNCTION__ and __LINE__ in Python, using the inspect module.

    def __line__ ():
        caller = inspect.stack()[1]
        return int (caller[2])
    
    def __function__ ():
        caller = inspect.stack()[1]
        return caller[3]

    我的测试代码
    import inspect
    
    def all():
    	caller = inspect.stack()
    	print len(caller)
    	print caller[0]
    	print caller[1]
    	print caller[2]
    
    def main():
    	all()
    
    if __name__=='__main__':
    	main()

    输出
    > "D:\Python25\python.exe"  "C:\Documents and Settings\xingming.xuxm\My Documents\Untitled 2.py" 
    3
    (<frame object at 0x00CBA368>, 'C:\\Documents and Settings\\xingming.xuxm\\My Documents\\Untitled 2.py', 6, 'all', ['\tcaller = inspect.stack()\n'], 0)
    (<frame object at 0x00CBBA38>, 'C:\\Documents and Settings\\xingming.xuxm\\My Documents\\Untitled 2.py', 13, 'main', ['\tall()\n'], 0)
    (<frame object at 0x00CAD8B0>, 'C:\\Documents and Settings\\xingming.xuxm\\My Documents\\Untitled 2.py', 16, '<module>', ['\tmain()'], 0)

  • 相关阅读:
    PagerIndicator主题样式修改
    写个Fragment方便的抽象基类 BaseFragment
    slidingMenu有时候需要关闭侧边栏
    通过构造方法传递数据
    三层ViewPager嵌套 的事件处理
    网络缓存的逻辑
    html中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
    AJAX请求
    animation-name
    Sublime Text 2主要快捷键列表
  • 原文地址:https://www.cnblogs.com/xuxm2007/p/1858152.html
Copyright © 2020-2023  润新知