What are type hints in Python 3.5?

后端 未结 5 1044
死守一世寂寞
死守一世寂寞 2020-11-21 11:01

One of the most talked about features in Python 3.5 is type hints.

An example of type hints is mentioned in this article and this o

5条回答
  •  悲哀的现实
    2020-11-21 11:45

    Adding to Jim's elaborate answer:

    Check the typing module -- this module supports type hints as specified by PEP 484.

    For example, the function below takes and returns values of type str and is annotated as follows:

    def greeting(name: str) -> str:
        return 'Hello ' + name
    

    The typing module also supports:

    1. Type aliasing.
    2. Type hinting for callback functions.
    3. Generics - Abstract base classes have been extended to support subscription to denote expected types for container elements.
    4. User-defined generic types - A user-defined class can be defined as a generic class.
    5. Any type - Every type is a subtype of Any.

提交回复
热议问题