Inspect params and return types

后端 未结 3 853
暖寄归人
暖寄归人 2021-01-13 21:26

Is it possible using Python 3 syntax for declaring input parameters and return value types determine those types? Similarly to determining the number of parameters of a func

3条回答
  •  不要未来只要你来
    2021-01-13 22:08

    inspect can be used:

    >>> def foo(name: str) -> int:
    ...     return 0
    >>> 
    >>> import inspect
    >>> 
    >>> sig = inspect.signature(foo)
    >>> [p.annotation for p in sig.parameters.values()]
    []
    >>> sig.return_annotation
    
    

    @vaultah's method looks even more convenient, though.

提交回复
热议问题