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):
...
, :
def foo(a, b, c):
...
, , , :
>>> 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)
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)?