What Is This Odd Colon Behavior Doing

What is this odd colon behavior doing?

You have accidentally written a syntactically correct variable annotation. That feature was introduced in Python 3.6 (see PEP 526).

Although a variable annotation is parsed as part of an annotated assignment, the assignment statement is optional:

annotated_assignment_stmt ::=  augtarget ":" expression ["=" expression]

Thus, in context["a"]: 2

  • context["a"] is the annotation target
  • 2 is the annotation itself
  • context["a"] is left uninitialised

The PEP states that "the target of the annotation can be any valid single assignment target, at least syntactically (it is up to the type checker what to do with this)", which means that the key doesn't need to exist to be annotated (hence no KeyError). Here's an example from the original PEP:

d = {}
d['a']: int = 0 # Annotates d['a'] with int.
d['b']: int # Annotates d['b'] with int.

Normally, the annotation expression should evaluate to a Python type --
after all the main use of annotations is type hinting, but it is not enforced. The annotation can be any valid Python expression, regardless of the type or value of the result.

As you can see, at this time type hints are very permissive and rarely useful, unless you have a static type checker such as mypy.

What is this odd colon behavior doing?

You have accidentally written a syntactically correct variable annotation. That feature was introduced in Python 3.6 (see PEP 526).

Although a variable annotation is parsed as part of an annotated assignment, the assignment statement is optional:

annotated_assignment_stmt ::=  augtarget ":" expression ["=" expression]

Thus, in context["a"]: 2

  • context["a"] is the annotation target
  • 2 is the annotation itself
  • context["a"] is left uninitialised

The PEP states that "the target of the annotation can be any valid single assignment target, at least syntactically (it is up to the type checker what to do with this)", which means that the key doesn't need to exist to be annotated (hence no KeyError). Here's an example from the original PEP:

d = {}
d['a']: int = 0 # Annotates d['a'] with int.
d['b']: int # Annotates d['b'] with int.

Normally, the annotation expression should evaluate to a Python type --
after all the main use of annotations is type hinting, but it is not enforced. The annotation can be any valid Python expression, regardless of the type or value of the result.

As you can see, at this time type hints are very permissive and rarely useful, unless you have a static type checker such as mypy.

What is this odd colon behavior doing?

You have accidentally written a syntactically correct variable annotation. That feature was introduced in Python 3.6 (see PEP 526).

Although a variable annotation is parsed as part of an annotated assignment, the assignment statement is optional:

annotated_assignment_stmt ::=  augtarget ":" expression ["=" expression]

Thus, in context["a"]: 2

  • context["a"] is the annotation target
  • 2 is the annotation itself
  • context["a"] is left uninitialised

The PEP states that "the target of the annotation can be any valid single assignment target, at least syntactically (it is up to the type checker what to do with this)", which means that the key doesn't need to exist to be annotated (hence no KeyError). Here's an example from the original PEP:

d = {}
d['a']: int = 0 # Annotates d['a'] with int.
d['b']: int # Annotates d['b'] with int.

Normally, the annotation expression should evaluate to a Python type --
after all the main use of annotations is type hinting, but it is not enforced. The annotation can be any valid Python expression, regardless of the type or value of the result.

As you can see, at this time type hints are very permissive and rarely useful, unless you have a static type checker such as mypy.

Shouldn't a:1 be a syntax error in python?

PEP-526 introduced variable annotations, which provide programmers a way to add type information to variables. This allows, among other things, statements like

x: int

to indicate that there is a local variable of type int, without initializing it. In PEP-484 - Acceptable Type Hints, we can see that annotations "must be valid expressions that evaluate without raising exceptions", which your dictionary literal is.

If you look at the Python grammar itself you can see that the expr_stmt and annassign rules make the example you show legal.

If you're using an IDE/other type hinting tools, they should definitely complain about this, but it doesn't break the rules that Python has set up.

Python colon : operator applied to a variable?

Credit to https://stackoverflow.com/users/67579/willem-van-onsem for his comment: looks like this is a way of initializing Dictionaries which I was not familiar. I've only done it the other two ways shown here: https://developmentality.wordpress.com/2012/03/30/three-ways-of-creating-dictionaries-in-python/



Related Topics



Leave a reply



Submit