我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用inspect.CO_VARARGS。
def inspect_func_args(fn): if py3k: co = fn.__code__ else: co = fn.func_code nargs = co.co_argcount names = co.co_varnames args = list(names[:nargs]) varargs = None if co.co_flags & CO_VARARGS: varargs = co.co_varnames[nargs] nargs = nargs + 1 varkw = None if co.co_flags & CO_VARKEYWORDS: varkw = co.co_varnames[nargs] if py3k: return args, varargs, varkw, fn.__defaults__ else: return args, varargs, varkw, fn.func_defaults
def get_args_index(target) -> int: """ Returns the index of the "*args" parameter if such a parameter exists in the function arguments or -1 otherwise. :param target: The target function for which the args index should be determined :return: The arguments index if it exists or -1 if not """ code = target.__code__ if not bool(code.co_flags & inspect.CO_VARARGS): return -1 return code.co_argcount + code.co_kwonlyargcount
def get_kwargs_index(target) -> int: """ Returns the index of the "**kwargs" parameter if such a parameter exists in the function arguments or -1 otherwise. :param target: The target function for which the kwargs index should be determined :return: The keyword arguments index if it exists or -1 if not """ code = target.__code__ if not bool(code.co_flags & inspect.CO_VARKEYWORDS): return -1 return ( code.co_argcount + code.co_kwonlyargcount + (1 if code.co_flags & inspect.CO_VARARGS else 0) )