首页 > 请问如何计算python中函数的参数数量?

请问如何计算python中函数的参数数量?

如果我定义了一个函数

def func(a,b,*others):
    ...  

之后在调用时如何计算输入参数的数量呢?
假如我想要实现"输入多于5个参数返回False"


你这里a,b是两个固定的参数,ohters是一个list,直接判断list的长度大于3就表示参数多于5个



剑走偏锋,可以试试inspect

# -*- coding: utf-8 -*-

import inspect


def foo(a, *args):
    f = inspect.currentframe()
    print inspect.getargvalues(f)
【热门文章】
【热门文章】