3

What does the following statement do in python?

x:5

Here the x is not even defined. The above statement runs without any error. Why does this work and what does it do?

I'm running the code on Python 3.8.3.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mitiku
  • 5,337
  • 3
  • 18
  • 35
  • 1
    It's a type hint. – Alan Bagel Sep 26 '21 at 21:07
  • 7
    That's a poorly-formed *type annotation* for the variable `x`. The thing to the right of the colon would properly be a type of some sort: `x: int` or `x: str` perhaps. But Python itself doesn't verify the annotations, it just makes them available for external tools, so no error is generated here. – jasonharper Sep 26 '21 at 21:08
  • 1
    A bad and, if I may say, a useless type hint, but a type hint nevertheless. https://www.python.org/dev/peps/pep-0483/ – navneethc Sep 26 '21 at 21:09

0 Answers0