在 Python 中获取第一个和最后一个函数参数

Ele*_*dum 2 python arguments function

我必须在 python 中创建一个具有 2 个以上参数的函数,最后我必须打印函数的第一个和最后一个参数(在列表中)。

我试过这样,但它不起作用。我究竟做错了什么?

import inspect

def func(a, b, c):
    frame = inspect.currentframe()
    args, _, _, values = inspect.getargvalues(frame)
    for i in args:
        return [(i, values[i]) for i=0 and i=n]
Run Code Online (Sandbox Code Playgroud)

Cil*_*yan 5

还有一种方法可以在 python 中获取可变数量的函数参数(称为var-positional)。然后它们最终变成一个列表:

def func(*args): # The trick here is the use of the star
    if len(args) < 3: # In case needed, also protects against IndexError
        raise TypeError("func takes at least 3 arguments") 
    return [args[0], args[-1]]
Run Code Online (Sandbox Code Playgroud)