17

As far as I know, now we can declare variables using type annotation syntax in Python 3.6 as following code.

def printInt():
    a: int = 0
    b: int = 1
    c: int = 2
    print(a, b, c)

What I want to do is declaring variables a, b, c in one line.

I tried a, b, c: int, but it returns error.

Also a: int=0, b: int=1, c: int=2 returns error too.

Is there any way to declare multiple variables using type annotation syntax in one line?

5
  • 1
    Well there really isn't static typing in the sense you want, the type annotations syntax may not support this case. There's always semicolons to put multiple lines on one line, but I feel like that's not what you are asking about? Commented Feb 19, 2018 at 6:11
  • @shuttle87 Well I was also talking about the type annotation. What I'm asking is how to assign values to multiple variables in one-line using type annotation syntax. Commented Feb 19, 2018 at 6:17
  • If you really asked just about 'how to assign values to multiple variable in one-line' your question is way of. In Python you assign values to variables. that's why you can't do a, b c you have to assign them as a,b,c=0,1,2 Commented Feb 19, 2018 at 6:25
  • @sdf3w yes, I understood you were talking about type annotations, currently I don't think multiple independent variables can be annotated on a single line: repl.it/repls/HelpfulTornBracket I'd like to do this myself in a couple of projects but I don't think it's possible (yet) Commented Feb 19, 2018 at 6:28
  • @shuttle87 So it seems it's not possible yet in current python implementation.. Thanks Commented Feb 19, 2018 at 6:34

4 Answers 4

7

If you really want to use annotation then you can do this in this form:-

a: int;b: int;c: int
a,b,c = range(3)
print(a,b,c) #As output 0 1 2
Sign up to request clarification or add additional context in comments.

Comments

6
 a: int = 0; b: int = 1; c: int = 2

would actually work. If you are looking for a way to avoid repeating int all times, I am afraid you cannot as of Python 3.7.

Comments

1

Commas are not allowed and will yield a syntax error. You can't use the regular one liner:

a, b, c = 0, 1, 2

The workarounds would work in any python version with typing support:

a: int; b: int; c: int; a, b, c = 0, 1, 2
a: int = 0; b: int = 1; c: int = 2

It does not apply to your question but this would work too in some cases:

abc: tuple[int] = (0, 1, 2)

1 Comment

The result is not as I would have wanted but in any case your answer is perfectly clear
-9

Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.

a,b,c=0,1,2

Or the following maybe what you are looking for

def magic(a: str, b: str) -> int:
    light = a.find(b)
    return light

3 Comments

Sorry for making question unclear. What I was talking about type annotation syntax actually, not static typing. I edited my question.
Edited my answer, let me know if this makes sense.
@Longroadahead No unfortunately it doesn't. He is asking about declaring and annotating multiple variables in a single line. Not sure what you were answering to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.