推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
poke707
V2EX  ›  Python

函数体里面能获取当前函数名称,被谁 call 等等的信息吗?

  •  
  •   poke707 ·
    Ginhing · Jun 19, 2014 · 3626 views
    This topic created in 4372 days ago, the information mentioned may be changed or developed.
    试下贴代码.能成不?
    http://gist.github.com/334765c1c24375c4073a

    在用ide调试的时候见到一些function object具有诸如func_name,func_code之类的.
    这些在运行之前能否引用?
    6 replies    2014-06-21 21:00:41 +08:00
    paulw54jrn
        1
    paulw54jrn  
       Jun 19, 2014
    http://stackoverflow.com/questions/10330119/how-to-backtrace-a-function-in-python-2-7
    搬运工

    import traceback

    def foo():
    bar()

    def bar():
    baz()

    def baz():
    traceback.print_stack()
    # or trace = traceback.extract_stack()

    foo()
    poke707
        2
    poke707  
    OP
       Jun 19, 2014
    @paulw54jrn 这个不太合适,我想的是在函数体内把"纯净的"函数状态引用出来
    比如foo=__func__这样
    YufunHe
        4
    YufunHe  
       Jun 19, 2014
    reflect
    poke707
        5
    poke707  
    OP
       Jun 19, 2014
    @miaoever 是的,大概可以这样 def f(a=0):return f.func_name
    @YufunHe 反射还是高深了点...也多谢你的提示
    xylophone21
        6
    xylophone21  
       Jun 21, 2014
    import traceback

    def foo():
    bar()


    def bar():
    foobar()

    def foobar():
    traceback.print_stack()

    if __name__ == '__main__':
    foo()


    是要这个吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1028 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 19:21 · PVG 03:21 · LAX 12:21 · JFK 15:21
    ♥ Do have faith in what you're doing.