V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2020-05-22 14:53:04 +08:00 · 2327 次点击
    这是一个创建于 1429 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在静态方法内部如何得知当前静态方法是哪一个类的?
    我在里面试着 dir() 了一下,MS 只有传递进来的参数。

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

    不过我觉得这种 trick 并不实用。最好还是从设计层面上避免这种奇怪的需求……
    jyyx
        2
    jyyx  
       2020-05-22 15:14:15 +08:00
    __class__ 看下是不是要这个
    YOUMA
        3
    YOUMA  
    OP
       2020-05-22 19:06:03 +08:00
    @InkStone @jyyx 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2795 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 14:39 · PVG 22:39 · LAX 07:39 · JFK 10:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.