2

I noticed that if I type, for instance >>> a: 5 as input of the python interpreter, it does not return an error (whether or not the variable 'a' is already defined). However, if I type >>> a afterwards, I get the usual NameError.

My question is: what does the python interpreter do when I type this kind of dictionary syntax without the curly braces?

Originally, I found this syntax in matplotlib's matplotlibrc file (see here).

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
johnc
  • 171
  • 4

2 Answers2

1

It defines a type hint. But without a value, the variable will not be initialized in the global scope.

>>> a: int = 3
>>> globals()['__annotations__']
{'a': <class 'int'>}
>>> a
3

>>> b: str
>>> globals()['__annotations__']
{'a': <class 'int'>, 'b': <class 'str'>}
>>> b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Richard Neumann
  • 2,986
  • 2
  • 25
  • 50
0

a: 5 in the interpreter uses the type hint syntax, but this has nothing to do with what you see in matplotlib's documentation that defines a configuration file (and thus is not python code).

from the documentation:

You can create custom styles and use them by calling style.use with the path or URL to the style sheet.

For example, you might want to create ./images/presentation.mplstyle with the following:

axes.titlesize : 24
axes.labelsize : 20
lines.linewidth : 3
lines.markersize : 10
xtick.labelsize : 16
ytick.labelsize : 16

The above is not python code

mozway
  • 194,879
  • 13
  • 39
  • 75