0

I stumbled over the following code:

x: int = 1.0

and I wonder: what does it do (and why does it work at all - I expect this to be a SyntaxError)?

The only thing I found out is that the tpye of x is still float, so it is not equivalent to x = int(1.0).

Qaswed
  • 3,649
  • 7
  • 27
  • 47
  • 1
    That's type annotation. Official doc: https://docs.python.org/3/library/typing.html – Andrej Kesely Jul 02 '19 at 07:51
  • 1
    Possible duplicate of [What are variable annotations in Python 3.6?](https://stackoverflow.com/questions/39971929/what-are-variable-annotations-in-python-3-6) – Georgy Jul 02 '19 at 07:53

1 Answers1

2

The answer can be found here: Function parameter with colon and here: What are variable annotations?

In short ": int" is just an annotation designed as a form of comment that many programs use to analyze your code. It is presumed to have a format and indicate the type of the variable but is ignored by the interpreter.