python - Function annotations -


i function annotations, because make code lot clearer. have question: how annotate function takes function argument? or returns one?

def x(f: 'function') -> 'function':     def wrapper(*args, **kwargs):         print("{}({}) has been called".format(f.__name__, ", ".join([repr(i) in args] + ["{}={}".format(key, value) key, value in kwargs])))         return f(*args, **kwargs)     return wrapper 

and don't want function = type(lambda: none) use in annotations.

use new typing type hinting support added python 3.5; functions callables, don't need function type, want can called:

from typing import callable,  def x(f: callable[..., any]) -> callable[..., any]:     def wrapper(*args, **kwargs):         print("{}({}) has been called".format(f.__name__, ", ".join([repr(i) in args] + ["{}={}".format(key, value) key, value in kwargs])))         return f(*args, **kwargs)     return wrapper 

the above specifies x takes callable object accepts arguments, , it's return type any, e.g. goes, generic callable object. x returns generic.

you express x(f: callable) -> callable: too; plain callable equivalent callable[..., any]. 1 pick style choice, used explicit option here personal preference.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -