What is the difference between hint types in 3.3 and 3.5?

I keep hearing how the hint type will be a new feature in 3.5, but it makes me wonder if the arrow indicator (->) was at 3.3?

You can see this in the 3.3 grammar specification here , which I found from this question asked 2 years ago.

I wonder if they hinted at hints before, but in a limited way, while 3.5 brings a lot of support? Or is my understanding of the hint type wrong, and does it really mean anything else?

+7
python type-hinting
source share
1 answer

For annotations, use -> . One use case for annotations is the type of hint.

Added annotations of Python 3.0, Python 3.5 builds on this function by introducing type hints, standardizing this function.

Relevant PEP (suggestions for improving Python):

Annotations are just syntax; a hint type is certain functionality.

The syntax can be used for anything you like, such as inline documentation:

 def documentation(self: "the instance", arg1: "first argument") -> "nothing is returned": pass 

All the syntax does is add the additional information provided to the function object:

 >>> def documentation(self: "the instance", arg1: "first argument") -> "nothing is returned": ... pass ... >>> documentation.__annotations__ {'return': 'nothing is returned', 'arg1': 'first argument', 'self': 'the instance'} 

The Type Hinting specification indicates how you could use these annotations to say something about what type each argument should have and what is returned. This particular application of annotations is that it defines how to interpret annotations.

The Hinting PEP type explicitly states that it is not intended to use only annotations:

Note that this PEP still does not explicitly prevent other uses of annotations and does not require (or prohibit) any specific processing of annotations, even if they comply with this specification. It just provides better coordination, like PEP 333 for web frameworks.

The hinting type remains completely optional, it will not and will never be needed for you to use it. Again quoting PEP:

While the proposed input module will contain some building blocks for checking the type of runtime, in particular the get_type_hints() function, it will be necessary to develop third-party packages to implement certain functions for checking the type of runtime, for example, using decorators or metaclasses. The type for optimizing performance remains as an exercise for the reader.

It should also be emphasized that Python will remain a dynamically typed language, and authors have no desire to ever make type hints mandatory, even by convention.

Emphasis in the original.

You can install the typing module to add a hint type to earlier versions of Python 3.x.

+12
source share

All Articles