获取python函数中参数的个数

获取python函数中参数的个数

问题描述:

我使函数获得函数中的参数个数,但它只给出数字时才给出数字,但发现我真正需要的是通过获取函数名称来获取数字。获取python函数中参数的个数

def a(a, b, c): 
    par = len(locals()) 
    return par 

z = a() 

我需要让z等于3,但它会给出错误。

def a(a, b, c): 
    par = len(locals()) 
    return par 

z = a(1, 2, 3) 

我需要机会得到len没有给出params。

Python允许你的函数使用argument unpacking接受一个arbitrary number of positional arguments

def a(*args): # allows any number of arguments without runtime errors 
    par = len(args) # args is a tuple of all the provided arguments 
    return par 

a(1, 2, 3) 
# 3 
a() 
# 0