If duplicate, please let me know, but I couldn't find an answer. Also, this question does not seem to address the issue.
I'd like to declare type of multiple variables in the same line. Instead of
a: float
b: float
c: float
I'd like to use something like
a, b, c: float
But I get a syntax error. What is the correct syntax?
It doesn't appear to be possible to annotate multiple variables with one annotation statement.
Annotated Assignment Statements defines an annotation as:
annotated_assignment_stmt ::= augtarget ":" expression
["=" (starred_expression | yield_expression)]
So the augtarget rule dictates what is allowed to go before the colon. augtarget is defined as:
augtarget ::= identifier | attributeref | subscription | slicing
So the only things that can go before the colon is an identifier (i.e. a single variable), an attributeref (an expression followed by .some_attribute_name), a subscription (an expression followed by [some_index]), or a slicing (same syntax as a subscription). a, b, c is not any of these things, so a, b, c: <some type> is not legal syntax.
If you merely want to annotate all three variables on one line, and not necessarily in one statement, you can chain independent simple statements together with a semicolon:
a:float; b:float; c:float
... But this is somewhat unsatisfying since you still have to type float three times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With