Type hinting in Python 2

后端 未结 3 1346
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 05:09

In PEP 484, type hinting was added to Python 3 with the inclusion of the typing module. Is there any way to do this in Python 2? All I can think of is having a decorator to

3条回答
  •  星月不相逢
    2020-11-27 05:55

    At this point the recommended and python3 compatible way to do is to follow the python2 to 3 guide : http://python-future.org/func_annotations.html

    def embezzle(self, account: str, funds: int = 1000000, *fake_receipts: str) -> None:
        """Embezzle funds from account using fake receipts."""
        pass
    

    Become:

    def embezzle(self, account, funds = 1000000, *fake_receipts):
        """Embezzle funds from account using fake receipts."""
        pass
    embezzle.__annotations__ = {'account': str, 'funds': int, 'fake_receipts': str, 'return': None}
    

提交回复
热议问题