Type hints and chained assignment and multiple assignments

£可爱£侵袭症+ 提交于 2019-12-05 06:52:16
  1. As explicitly stated in PEP 526, section "Rejected/postponed proposals", annotations in chained assignments are not supported. Quoting the PEP:

    This has problems of ambiguity and readability similar to tuple unpacking, for example in:
    x: int = y = 1
    z = w: int = 1
    it is ambiguous, what should the types of y and z be? Also the second line is difficult to parse.

  2. For unpacking, per the same PEP, you should place bare annotations for your variables before the assignment. Example from the PEP:

    # Tuple unpacking with variable annotation syntax
    header: str
    kind: int
    body: Optional[List[str]]
    header, kind, body = message
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!