1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it a python 3.6 thing? I don't see it in 3.5.2
@RickyHan yup. You should get a SyntaxError in 3.5.2 (and __annotations__ for a module don't exist, either)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.