Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply typing to python destructuring assignment [duplicate]

Is it possible to use type hinting when unpacking a tuple? I want to do this, but it results in a SyntaxError:

from typing import Tuple

t: Tuple[int, int] = (1, 2)
a: int, b: int = t
#     ^ SyntaxError: invalid syntax
like image 358
Cai Avatar asked Dec 08 '25 18:12

Cai


1 Answers

According to PEP-0526, you should annotate the types first, then do the unpacking

a: int
b: int
a, b = t
like image 161
Patrick Haugh Avatar answered Dec 11 '25 11:12

Patrick Haugh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!