推荐学习书目
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 · May 29, 2020 · 2289 views
    This topic created in 2176 days ago, the information mentioned may be changed or developed.
    class Worker:
        def work(self, callback: Callable):
            # 如何判断 callback 隐含参数 self 是自己?
            pass
    
        def rest(self):
            pass
    
    
    def todo():
        worker = Worker()
        worker.work(worker.rest)
    
    
    
    2 replies    2020-05-29 11:38:59 +08:00
    anzu
        1
    anzu  
    OP
       May 29, 2020   ❤️ 1
    我懂了,这样
    if getattr(callback, '__self__', None) is self:
    print('callback is self')
    ranleng
        2
    ranleng  
       May 29, 2020
    不知道对不对.

    Worker().work 的 __class__ 是 method
    todo 的 __class__ 是 function
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5541 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 08:23 · PVG 16:23 · LAX 01:23 · JFK 04:23
    ♥ Do have faith in what you're doing.