Why do we need function annotations for highlighted syntax in Python?

Functional annotations seem to duplicate the behavior already found in Python. Not only that, the meaning that they accept does not apply in any way, so they can be used for any of the following described in PEP 3107

  • Typing information
  • Type checking
  • Let the IDE show what types the function expects and returns
  • Function overload / general functions
  • Bridges in foreign languages
  • Adaptation
  • Predicate logic functions
  • Database query mapping
  • RPC Parameter Routing
  • Other information
  • Documentation for parameters and return values

or even something completely different.

, Python:

, Python :

>
>     if foo: #{
>         foo1();
>         foo2();
>         foo3();
>     #}

,

def foo(a: 'x', b: 5 + 6, c: list) -> max(2, 9):
    ...

, :

# a: 'x', b: 5 + 6, c: list
def foo(a, b, c):  #-> max(2, 9):
    ...

, , , :

>>> def spam(a: 'eggs') -> 'ni!':
...     pass
...
>>> spam.__annotations__
{'a': 'eggs', 'return': 'ni!'}

, :

def param(**kw):
    def decorator(func):
        def wrap(*args):
            print kw
            func(*args)
        return wrap
    return decorator

def return_(arg):
    def decorator(func):
        def wrap(*args):
            func(*args)
            print arg
        return wrap
    return decorator

@param(a='eggs')
@return_('ni!')
def spam(a):
    pass

spam(None)

# Output:
# -------
## {'a': 'eggs'}
## ni!

Python , , ?

EDIT: , .

, ,

@decorator
def spam():
    pass

def spam():
    pass
spam = decorator(spam)

,

self.method(param)

Class.method(self, param)

. , , ; . ,

def spam() -> int:
    pass

def spam() -> 'integer':
    pass

, . , , , . , , .

, :

, ? , , , (PEP 3107)?

+5
2

PEP 3107, , , "":

Python 2.x , . "PEP 318", docstring, .

PEP , , , .

+6

? , :

@staticmethod
def foo():
    pass

def foo():
    pass
foo = staticmethod(foo)

.

, :

@param(a='eggs')
@return_('ni!')
def spam(a):
    pass

def spam(a: 'eggs') -> 'ni!':
    pass

, , .

+6

All Articles