0

I want to define a function using explicit argument names ff(a,b,c) in the function definition, but I also want to map a function over all arguments to get a list:

ff(a,b,c):
    return list(map(myfunc,[a,b,c]))

However, I don't want to explicitly write parameter names inside function as a,b,c. I want to do it like

ff(a,b,c):
    return list(map(myfunc,getArgValueList()))

getArgValueList() will retrieve the argument values in order and form a list. How to do this? Is there a built-in function like getArgValueList()?

20
  • Use *args as the parameter. Commented May 14, 2018 at 3:02
  • 1
    @Code-Apprentice The OP said "I want to argument interface to be explict. That is explict write argument names as ff(a,b,c) when making function definition", and even edited the question to put that in bold after you made your comments on the answer, so… I think, even if maybe that's what the OP should want, it's not what the OP does want. Commented May 14, 2018 at 3:14
  • 1
    @smci I don't think it is a duplicate. What the OP wants to do is to define a function with explicit parameters, but then get the args list anyway. That's an unusual thing to do, so it's not going to match the obvious dups involving varargs or splats, even if it looks like it at first glance. Commented May 14, 2018 at 3:43
  • 2
    @PM2Ring This is even less idiomatic Java than Python. It's… maybe (pre-ES5) JavaScript, but really it feels more Perl than anything else. Commented May 14, 2018 at 6:19
  • 1
    @abarnert Ok. I was just taking a guess at it being a Java thing, mostly from the camelCase function name (I don't write Java). Commented May 14, 2018 at 6:29

2 Answers 2

2

What you're trying to do is impossible without ugly hacks. You either take *args and get a sequence of parameter values that you can use as args:

def ff(*args):
    return list(map(myfunc, args))

… or you take three explicit parameters and use them by name:

def ff(a, b, c):
    return list(map(myfunc, (a, b, c)))

… but it's one or the other, not both.

Of course you can put those values in a sequence yourself if you want:

def ff(a, b, c):
    args = a, b, c
    return list(map(myfunc, args))

… but I'm not sure what that buys you.


If you really want to know how to write a getArgValueList function anyway, I'll explain how to do it. However, if you're looking to make your code more readable, more efficient, more idiomatic, easier to understand, more concise, or almost anything else, it will have the exact opposite effect. The only reason I could imagine doing something like this is if you had to generate functions dynamically or something—and even then, I can't think of a reason you couldn't just use *args. But, if you insist:

def getArgValueList():
    frame = inspect.currentframe().f_back
    code = frame.f_code
    vars = code.co_varnames[:code.co_argcount]
    return [frame.f_locals[var] for var in vars]

If you want to know how it works, most of it's in the inspect module docs:

  • currentframe() gets the current frame—the frame of getArgValueList.
  • f_back gets the parent frame—the frame of whoever called getArgValueList.
  • f_code gets the code object compiled from the function body of whoever called getArgValueList.
  • co_varnames is a list of all local variables in that body, starting with the parameters.
  • co_argcount is a count of explicit positional-or-keyword parameters.
  • f_locals is a dict with a copy of the locals() environment of the frame.

This of course only works for a function that takes no *args, keyword-only args, or **kwargs, but you can extend it to work for them as well with a bit of work. (See co_kwonlyargcount, co_flags, CO_VARARGS, and CO_VARKEYWORDS for details.)

Also, this only works for CPython, not most other interpreters. and it could break in some future version, because it's pretty blatantly relying on implementation details of the interpreter.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, @abarnert. Thank you so much for taking so much time answering. I learned a lot. But it seems that getArgValueList is not working properly. getArgValueList(1,2,3) gives () if I defined it as def getArgValueList(a,b,c):
@user15964 Why did you define it as getArgValueList(a,b,c). You wanted a function that you can call as getArgValueList() from any function. That's what I gave you. And it works. If randomly changing it into something different without understanding it doesn't work, that's not surprising.
Oh, I am so sorry. My brain must ran into chaos previously. It indeed works. Thank you so much, and I decide to accept your answer, because you provide explicit getArgValueList. Though jferard's solution also works, it seems that it is not easy to construct getArgValueList via his approach.
@user15964 Yes, Python makes it possible to do all kinds of clever things, but the ones that are usually too clever to be a good idea, it usually makes a bit unpleasant.
1

The *args construction will give you the arguments as a list:

>>> def f(*args): return list(map(lambda x:x+1, args))
>>> f(1,2,3)
[2, 3, 4]

If you are bound with the signature of f, you'll have to use the inspect module:

import inspect

def f(a, b,c):
    f_locals = locals()
    values = [f_locals[name] for name in inspect.signature(f).parameters]
    return list(map(lambda x:x+1, values))

inspect.signature(f).parameters gives you the list of arguments in the correct order. The values are in locals().

12 Comments

Shouldn't it be print(f(1, 2, 3))?
@Code-Apprentice @jferard Thank you so much. But actually I want the argument interface be explicit. So I must write ff(a,b,c)
@Code-Apprentice If should be f(1,2,3), but also the function should be list(map(lambda x:x+1, args)).
As written, this function doesn't take 3 arguments, it takes one argument that's a list of 3 elements. If you want to do that, you don't need splat at all—just def f(lst): return list(map(lambda x: x+1, lst)).
@user15964 You have to write ff(a,b,c) when you call the function or when you define it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.