Reputation: 500
When I type aaa: print(1)
in Python 3.6, it will print 1
without any error.
I want to know what variable:expression means in Python.
I Googled and cannot find any documentation related to this.
Upvotes: 1
Views: 70
Reputation: 160407
It's a variable annotation, as described in PEP 526. By running that expression, you've annotated the type of a
to None
, the return value of the print
call, which doesn't make much sense.
You can see this by printing the __annotations__
, a dictionary that holds the relation between names-types for a module (in your case, the module will probably be __main__
):
print(__annotations__)
{'aaa': None}
Python doesn't do anything with these, it simply executed the print(1)
(resulting in the output of 1
you see) expression and uses the return value of that call to annotate the name a
. It's up to type-checkers, like mypy
, to use them for their own purposes.
Upvotes: 2