推荐学习书目
› 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
YOUMA
V2EX  ›  Python

请问怎样在静态方法中获得当前调用的上下文?

  •  
  •   YOUMA · May 22, 2020 · 3459 views
    This topic created in 2321 days ago, the information mentioned may be changed or developed.
    在静态方法内部如何得知当前静态方法是哪一个类的?
    我在里面试着 dir() 了一下,MS 只有传递进来的参数。

    请高人指点,谢谢。
    3 replies  •  2020-05-22 19:06:03 +08:00
    InkStone
        1
    InkStone  
       May 22, 2020   ❤️ 1
    traceback.print_stack 可以看到调用栈。你可以去看看它的实现里是怎么做的。

    不过我觉得这种 trick 并不实用。最好还是从设计层面上避免这种奇怪的需求……
    jyyx
        2
    jyyx  
       May 22, 2020
    __class__ 看下是不是要这个
    YOUMA
        3
    YOUMA  
    OP
       May 22, 2020
    @InkStone @jyyx 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1297 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 23:59 · PVG 07:59 · LAX 16:59 · JFK 19:59
    ♥ Do have faith in what you're doing.