Nelio Alves
Nelio Alves

Reputation: 1371

Python: declare type of multiple variables in the same line

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?

Upvotes: 12

Views: 5937

Answers (4)

Jan Heldal
Jan Heldal

Reputation: 176

It would indeed be nice if PEP484 type-hints supported multiple declarations of same type as a single statement, and also if it supported type-hinting while unwrapping Tuples.

Barring that, I found dummy assignments to be useful as a work-around when wanting to declare multiple variables of the same type in a single statement:

# Multiple type-declarations on same line, but not same statement:
a: List[int]; b: Tuple[float, ...]; c: Iterable[str]
# Multiple declarations of same type in same statement:
# noinspection PyUnusedLocal
k = l = m = n = 0  # type: int
# noinspection PyUnusedLocal
q = r = s = t = ''  # type: str
# noinspection PyUnusedLocal
x = y = z = w = 0.0  # type: float

Explanation: Even if the initial dummy values are never used (because the variables will be overwritten), they still ensure that those variables are understood to have the type of the value first given to them. (I also added old-style comment type hints to make the intention clear to human readers. But removing them makes no difference. PyCharm still understands.)

I am using PyCharm 2021.2.1 with Python 3.8. (PyCharm has its own linter implementing PEP484, separate from mypy.) (Also, I have just begun learning Python this summer, so this answer can probably be improved.)

Upvotes: 0

Kevin
Kevin

Reputation: 76184

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.

Upvotes: 16

Rajeshkumar
Rajeshkumar

Reputation: 59

You have to assign all the values for all the variables.

E.G:-
a,b,c = 1.0,2.0,3.0
a,b,c = 1.0,1.0,1.0

Upvotes: -5

Elliott Bryant
Elliott Bryant

Reputation: 1

 a,b,c: float,float,float

Would be correct

Upvotes: -4

Related Questions