Well we don't actually need inspect here.
>>> func = lambda x, y: (x, y)
>>>
>>> func.__code__.co_argcount
2
>>> func.__code__.co_varnames
('x', 'y')
>>>
>>> def func2(x,y=3):
... print(func2.__code__.co_varnames)
... pass # Other things
...
>>> func2(3,3)
('x', 'y')
>>>
>>> func2.__defaults__
(3,)
locals() returns a dictionary with local names:
def func(a, b, c):
print(locals().keys())
prints the list of parameters. If you use other local variables those will be included in this list. But you could make a copy at the beginning of your function.
Getting all arguments from a function call in python
python - How to print a function's arguments in the correct order? - Stack Overflow
Maximum number of arguments in a print function
Quick question for getting all arguments except last one
You don't need the arithmetic notation at all.
echo "${@:1:$#-1}"
nor do you need $, for a regular variable (only if explicit notation is required, as in ${#var} or ${str%%-*}). This goes for the index of an indexed array also, and no $ required inside arithmetic, eg:
num=4
echo "${array[num+2]} is the 7th element of the array (aka the value of array index 6)"
echo "${@:num:2} is the 4th and 5th argument to the script"
((num>0)) && echo "$num is a positive integer"
Note that if you use printf instead of echo, you'll have full control over the separator character, between the arguments (eg. printf '%s\n' "${@:1:$#-1} prints all args (but the last) on a new line.
Check out the parameter substitution section in man bash for more relevant info.
Videos
Hello,
Im trying to find out if there is a way to access all arguments passed to a function (perhaps in a dictionary) from within a function? Does anyone know a way to do this?
For example if I have
def myfunc(a,b,c,d):
passIs there some line of code I can use that will let me gather all the arguments passed into a list or a dictionary?
Thanks