计算有多少参数作为位置传递

mar*_*lli 39 python function parameter-passing

如果我有一个功能

def foo(x, y):
    pass
Run Code Online (Sandbox Code Playgroud)

我如何从函数内部判断y是按位置传递还是使用其关键字传递?

我想要类似的东西

def foo(x, y):
    if passed_positionally(y):
        print('y was passed positionally!')
    else:
        print('y was passed with its keyword')
Run Code Online (Sandbox Code Playgroud)

所以我得到

>>> foo(3, 4)
y was passed positionally
>>> foo(3, y=4)
y was passed with its keyword
Run Code Online (Sandbox Code Playgroud)

我意识到我最初没有指定这一点,但是可以在保留类型注释的同时做到这一点吗?到目前为止的最佳答案建议使用装饰器 - 但是,这不会保留返回类型

Cyt*_*rak 48

您可以创建一个装饰器,如下所示:

def checkargs(func):
    def inner(*args, **kwargs):
        if 'y' in kwargs:
            print('y passed with its keyword!')
        else:
            print('y passed positionally.')
        result = func(*args, **kwargs)
        return result
    return inner

>>>  @checkargs
...: def foo(x, y):
...:     return x + y

>>> foo(2, 3)
y passed positionally.
5

>>> foo(2, y=3)
y passed with its keyword!
5
Run Code Online (Sandbox Code Playgroud)

当然,您可以通过允许装饰器接受参数来改进这一点。因此,您可以传递要检查的参数。这将是这样的:

def checkargs(param_to_check):
    def inner(func):
        def wrapper(*args, **kwargs):
            if param_to_check in kwargs:
                print('y passed with its keyword!')
            else:
                print('y passed positionally.')
            result = func(*args, **kwargs)
            return result
        return wrapper
    return inner

>>>  @checkargs(param_to_check='y')
...: def foo(x, y):
...:     return x + y

>>> foo(2, y=3)
y passed with its keyword!
5
Run Code Online (Sandbox Code Playgroud)

我认为添加functools.wraps会保留注释,以下版本还允许对所有参数执行检查(使用inspect):

from functools import wraps
import inspect

def checkargs(func):
    @wraps(func)
    def inner(*args, **kwargs):
        for param in inspect.signature(func).parameters:
            if param in kwargs:
                print(param, 'passed with its keyword!')
            else:
                print(param, 'passed positionally.')
        result = func(*args, **kwargs)
        return result
    return inner

>>>  @checkargs
...: def foo(x, y, z) -> int:
...:     return x + y

>>> foo(2, 3, z=4)
x passed positionally.
y passed positionally.
z passed with its keyword!
9

>>> inspect.getfullargspec(foo)
FullArgSpec(args=[], varargs='args', varkw='kwargs', defaults=None, 
kwonlyargs=[], kwonlydefaults=None, annotations={'return': <class 'int'>})
                                             _____________HERE____________
Run Code Online (Sandbox Code Playgroud)


Aso*_*cia 14

最后,如果你要做这样的事情:

def foo(x, y):
    if passed_positionally(y):
        raise Exception("You need to pass 'y' as a keyword argument")
    else:
        process(x, y)
Run Code Online (Sandbox Code Playgroud)

你可以这样做:

def foo(x, *, y):
    pass

>>> foo(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() takes 1 positional argument but 2 were given

>>> foo(1, y=2) # works
Run Code Online (Sandbox Code Playgroud)

或者只允许它们在位置上传递:

def foo(x, y, /):
    pass

>>> foo(x=1, y=2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() got some positional-only arguments passed as keyword arguments: 'x, y'

>>> foo(1, 2) # works
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅PEP 570PEP 3102


mar*_*lli 5

改编自@Cyttorak 的回答,这是一种维护类型的方法:

from typing import TypeVar, Callable, Any, TYPE_CHECKING

T = TypeVar("T", bound=Callable[..., Any])

from functools import wraps
import inspect

def checkargs() -> Callable[[T], T]:
    def decorate(func):
        @wraps(func)
        def inner(*args, **kwargs):
            for param in inspect.signature(func).parameters:
                if param in kwargs:
                    print(param, 'passed with its keyword!')
                else:
                    print(param, 'passed positionally.')
            result = func(*args, **kwargs)
            return result
        return inner
    return decorate

@checkargs()
def foo(x, y) -> int:
    return x+y

if TYPE_CHECKING:
    reveal_type(foo(2, 3))
foo(2, 3)
foo(2, y=3)
Run Code Online (Sandbox Code Playgroud)

输出是:

$ mypy t.py 
t.py:27: note: Revealed type is 'builtins.int'
Run Code Online (Sandbox Code Playgroud)
$ python t.py 
x passed positionally.
y passed positionally.
x passed positionally.
y passed with its keyword!
Run Code Online (Sandbox Code Playgroud)