-1

How can it be explained that inspect.getargvalues returns keyword only args as args instead of varargs. Is this a bug or a documentation bug? Are keyword only args not keyword args? I don't understand.

inspect.getargvalues(frame)

Get information about arguments passed into a particular frame. A named tuple ArgInfo(args, varargs, keywords, locals) is returned. args is a list of the argument names. varargs and keywords are the names of the * and ** arguments or None. locals is the locals dictionary of the given frame.

import inspect
def fun(x, *, y):
    print (inspect.getargvalues(inspect.currentframe()))

Output:

fun (10, y=20)
ArgInfo(args=['x', 'y'], varargs=None, keywords=None, locals={'y': 20, 'x': 10})

1 Answers1

0

As it says: "varargs and keywords are the names of the * and ** arguments". Your function doesn't have any * or ** arguments.

The * that appears here:

def fun(x, *, y):

merely serves as a separator between positional arguments and keyword-only arguments.

This would be an example of a function where varargs and keywords would be set:

def fun(*x, **y):
    print(inspect.getargvalues(inspect.currentframe()))

which would produce:

>>> fun(10, y=20)
ArgInfo(args=[], varargs='x', keywords='y', locals={'x': (10,), 'y': {'y': 20}})
sj95126
  • 6,520
  • 2
  • 15
  • 34
  • I understand what you say and therefor i accept the answer. However, it is very confusing to me why `**kwargs` are classified as keyword args but the keyword-only args behind `*` not. It is very straightforword to conclude that `keywords` stand for keyword-args and `inspect` handles them unexpectedly different. How can such inconsistecy be accepted by the community? – Wör Du Schnaffzig Oct 11 '21 at 10:27