Python get all function arguments

Get a list/tuple/dict of the arguments passed to a function?

How would one obtain a list/tuple/dict/etc of the arguments passed in, without having to build the structure myself? Specifically, I’m looking for Python’s version of JavaScript’s arguments keyword or PHP’s func_get_args() method. What I’m not looking for is a solution using *args or **kwargs ; I need to specify the argument names in the function definition (to ensure they’re being passed in) but within the function I want to work with them in a list- or dict-style structure.

Please Do Not Do This. The folks that maintain your code will be absolutely baffled by creating a useless dictionary out of perfectly good keyword arguments. They will be forced to rewrite to remove the dictionary.

@blahdiblah This question is asking how to obtain the parameters themselves, as opposed to the names of the parameters. I don’t think it’s a duplicate.

@S.Lott: That’s a rather narrow-minded view. Sometimes one wants to build a dictionary inside a function, initialised with some compulsory parameters. The function argument list makes it clear what those are, and believe me, they’re not necessarily ‘useless’. Please refer to Python’s DRY principle, and don’t be like some junior devs I’ve worked with, who whinge ‘why would anybody want to do THAT?’ when I suggest their API could be more versatile.

8 Answers 8

You can use locals() to get a dict of the local variables in your function, like this:

def foo(a, b, c): print locals() >>> foo(1, 2, 3)

This is a bit hackish, however, as locals() returns all variables in the local scope, not only the arguments passed to the function, so if you don’t call it at the very top of the function the result might contain more information than you want:

def foo(a, b, c): x = 4 y = 5 print locals() >>> foo(1, 2, 3)

I would rather construct a dict or list of the variables you need at the top of your function, as suggested in the other answers. It’s more explicit and communicates the intent of your code in a more clear way, IMHO.

Читайте также:  Сырая строка в питоне

Thanks. For anyone else looking to use this method, bear in mind that you should use locals() directly after the function definition, otherwise it will contain all variables defined within the function.

It’s interesting to note that locals() returns a snapshot of local variables, not a pointer to some built-in dictionary. So if variables are defined later or a parameter is changed within the function, whatever locals() was assigned to won’t change. This is what you want. (I 💜 Python.)

Also interesting to note that if you use locals() in a method, one of the keys in the dict is ‘self’. So, make sure you correct / account for this.

Here’s a function you can call to get the kwargs of the current function. Or if you want to use those lines directly in your own function instead of calling get_kwargs() just remove the .f_back

import inspect def get_kwargs(): frame = inspect.currentframe().f_back keys, _, _, values = inspect.getargvalues(frame) kwargs = <> for key in keys: if key != 'self': kwargsPython get all function arguments = valuesPython get all function arguments return kwargs def test(x, y=100): z = 7 print(get_kwargs()) test(5, 6) # Output: test(5) # Output:

I thought this was the obvious right answer because it is explicit, but in looking at the documentation, inspect.getargvalues 4th argument returns locals() of the current frame. So effectively, this calls a function that gets the frame of its calling environment to return locals() — seems a bit circuitous. On the other hand, it allows you do other things in the function, like exclude some arguments. Thanks for the submission!

You can use the inspect module:

def foo(x): return x inspect.getargspec(foo) Out[23]: ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None) 

This is a duplicate of this and this.

I would use *args or **kwargs and throw an exception if the arguments are not as expected

If you want to have the same errors than the ones checked by python you can do something like

def check_arguments(function_name,args,arg_names): missing_count = len(arg_names) - len(args) if missing_count > 0: if missing_count == 1: raise TypeError(function_name+"() missing 1 required positionnal argument: "+repr(arg_names[-1])) else: raise TypeError(function_name+"() missing "+str(missing_count)+" required positionnal argument: "+", ".join([repr(name) for name in arg_names][-missing_count:-1])+ " and "+repr(arg_names[-1])) 
def f(*args): check_arguments("f",args,["a","b","c"]) #whatever you want . 

That would require lots of testing within the method body, while defining the parameters as part of the function would raise similar errors without any additional tests.

From the accepted answer from a duplicate (older??) question https://stackoverflow.com/a/582206/1136458 :

 frame = inspect.currentframe() args, _, _, values = inspect.getargvalues(frame) 
class my_class(): compulsory_default_kwargs = formal_args_names = ['var1','var2'] def __init__(self,*args,**kwargs): for key , value in locals().items(): if((key == 'args')): if(len(args) == len(my_class.formal_args_names)): for key_name , arg_value in zip(my_class.formal_args_names,args): setattr(self,key_name,arg_value) else: raise Exception ('Error - Number of formal arguments passed mismatched required <> whereas passed <>'.format(len(my_class.formal_args_names),len(args))) elif((key == 'kwargs') & (len(kwargs)!=0)): for kw_key , kw_value in kwargs.items(): if kw_key in my_class.compulsory_default_kwargs.keys(): setattr(self,kw_key,kw_value) else: raise Exception ('Invalid key-word argument passed <>'.format(kw_key)) #elif((key!='self') & (key!='kwargs') ): # setattr(self,key,value) for key , value in my_class.compulsory_default_kwargs.items(): if key not in kwargs.keys(): setattr(self,key,value) this_value = 'Foo' my_cl = my_class(3,this_value,p='B',r=10) my_cl.__dict__ 

You’ve specified the parameters in the header?

Why don’t you simply use that same info in the body?

def foo(a, b, c): params = [a, b, c] 

This isn’t analogous to JavaScript’s arguments or PHP’s func_get_args() , which is what I’m looking for.

I understand that. What is missing is why you would want a function like that. What is wrong with using the information you already have?

He might want to abstract it for one, so he writes a helper that works with ALL functions, not having to write each of them params manually all the time. E.g. log_methods_arguments(arguments)

@Nikos: You log_methods_arguments example could take *args, **kwargs parameters. The OP says the exact params must be specified. The motivation for this code smell is unclear.

log_methods_arguments taking *args, **kwargs wouldn’t change anything. You still need to get the arguments list from the method that you want to log, the one that calls log_method_arguments.

Источник

Accessing all function argmuments

I have a function with 4 arguments and want to check those 4 arguments for something. Currently I do it like this:

def function1(arg1, arg2, arg3, arg4): arg1 = function2(arg1) arg2 = function2(arg2) arg3 = function2(arg3) arg4 = function2(arg4) def function2(arg): arg = dosomething(arg) return arg 
def function1(arg1, arg2, arg3, arg4): for arg in listOfArguments: arg = function2(arg) def function2(arg): arg = checkSomething(arg) return arg 

Is there a way in Python to get a list of all the arguments passed to function1 ? Thanks for your help and ideas.

2 Answers 2

Since you have a set number of arguments just create an iterable out of them, for example, wrap the argument names in a tuple literal:

for arg in (arg1, arg2, arg3, arg4): # do stuff 

If you don’t mind your function being capable of being called with more args just use *args syntax:

def function1(*args): for arg in args: arg = function2(arg) 

which then exposes the arguments with which the function was invoked as a tuple which can be iterated over.

If you need to store the results for every invocation, it is better to change the for-loop in a list comprehension creating a list of the returned values. For example, given a function that takes a value and simply adds 2 to it:

def add2(arg): return arg + 2 def function1(arg1, arg2, arg3, arg4): a1, a2, a3, a4 = [add2(arg) for arg in (arg1, arg2, arg3, arg4)] print(a1, a2, a3, a4) 

We create a list comprehension that takes every arg and supplies it to add2 and then stores it as an entry in a list, then we unpack to a1. aN . The values now contain the results of the function calls:

In the previous examples (arg1, arg2, arg3, arg4) can always be replaced with args if you use the *args syntax when defining the function, the results will be similar.

As an addendum, if you’re more a fan of functional programming, you could always map the arguments to the function and save a few characters :-). I’ll add a *args definition of function1 this time:

def function1(*args): a1, a2, a3, a4 = map(add2, args) print(a1, a2, a3, a4) 

Источник

How to get complete list of arguments for function from module in python

I am using iPython in command prompt, Windows 7. I thought this would be easy to find, I searched and found directions on how to use the inspect package but it seems like the inspect package is meant to be used for functions that are created by the programmer rather than functions that are part of a package. My main goal to to be able to use the help files from within command prompt of iPython, to be able to look up a function such as csv.reader() and figure out all the possible arguments for it AND all possible values for these arguements. In R programming this would simply be args(csv.reader()) I have tried googling this but they all point me to the inspect package, perhaps I’m misunderstanding it’s use? For example, If I wanted to see a list of all possible arguments and the corresponding possible values for these arguments for the csv.reader() function (from the import csv package), how would I go about doing that? I’ve tried doing help(csv.reader) but this doesn’t provide me a list of all possible arguments and their potential values. ‘Dialect’ shows up but it doesn’t tell me the possible values of the dialect argument of the csv.reader function. I can easily go to the site: https://docs.python.org/3/library/csv.html#csv-fmt-params and see that the dialect options are: delimiter, doublequote, escapechar, etc.. etc..but is there a way to see this in Python console? I’ve also tried dir(csv.reader) but this isn’t what I was looking for either. Going bald trying to figure this out.

I don’t think this is a programming problem so much as a general frustration with documentation. help prints and formats the docstrings which are only as helpful as they’ve been written to be. There is no general solution to show «all possible arguments» and «all possible values for said arguments» in Python because that’s not even enumerable in many cases (e.g., variadic functions).

Nearly all the documentation in library comes directly from the DocStrings: help(csv.Dialect) would give you the details you are looking for, unfortunately as pointed out the help is only as good as the docstrings and the docstring for csv.reader doesn’t clearly point you to csv.Dialect . You can then look at the specific dialects, e.g. help(csv.excel) . Introspection on the arguments doesn’t inform you of what the valid values would be so I can’t see how this would help.

Источник

Оцените статью