Ciro Santilli OurBigBook.com  Sponsor 中国独裁统治 China Dictatorship 新疆改造中心、六四事件、法轮功、郝海东、709大抓捕、2015巴拿马文件 邓家贵、低端人口、西藏骚乱
python/typing_cheat/protocol.py
#!/usr/bin/env python3

from typing import Protocol

class CanFly(Protocol):
    def fly(self) -> str:
        pass

    def fly_fast(self) -> str:
        return 'CanFly.fly_fast'

class Bird(CanFly):
    def fly(self):
        return 'Bird.fly'
    def fly_fast(self):
        return 'Bird.fly_fast'

class FakeBird(CanFly):
    pass

assert Bird().fly() == 'Bird.fly'
assert Bird().fly_fast() == 'Bird.fly_fast'
assert FakeBird().fly() is None
assert FakeBird().fly() == ''

Ancestors (12)

  1. Python Protocol
  2. Python typing
  3. Python standard library
  4. Python
  5. List of programming languages
  6. Programming language
  7. Software
  8. Computer
  9. Information technology
  10. Area of technology
  11. Technology
  12. Ciro Santilli's Homepage