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
anzu
V2EX  ›  Python

如何判断 callable 隐含参数 self 是自己?

  •  
  •   anzu · 2020-05-29 11:23:12 +08:00 · 1846 次点击
    这是一个创建于 1421 天前的主题,其中的信息可能已经有所发展或是发生改变。
    class Worker:
        def work(self, callback: Callable):
            # 如何判断 callback 隐含参数 self 是自己?
            pass
    
        def rest(self):
            pass
    
    
    def todo():
        worker = Worker()
        worker.work(worker.rest)
    
    
    
    2 条回复    2020-05-29 11:38:59 +08:00
    anzu
        1
    anzu  
    OP
       2020-05-29 11:35:48 +08:00   ❤️ 1
    我懂了,这样
    if getattr(callback, '__self__', None) is self:
    print('callback is self')
    ranleng
        2
    ranleng  
       2020-05-29 11:38:59 +08:00
    不知道对不对.

    Worker().work 的 __class__ 是 method
    todo 的 __class__ 是 function
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2169 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 00:27 · PVG 08:27 · LAX 17:27 · JFK 20:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.