Passing on named variable arguments in python

Say I have the following methods:

def methodA(arg, **kwargs):
    pass

def methodB(arg, *args, **kwargs):
    pass

In methodA I wish to call methodB, passing on the kwargs. However, it seems that if I define methodA as follows, the second argument will be passed on as positional rather than named variable arguments.

def methodA(arg, **kwargs):
    methodB("argvalue", kwargs)

How do I make sure that the **kwargs in methodA gets passed as **kwargs to methodB?


Asked by: Daisy301 | Posted: 28-01-2022






Answer 1

Put the asterisks before the kwargs variable. This makes Python pass the variable (which is assumed to be a dictionary) as keyword arguments.

methodB("argvalue", **kwargs)

Answered by: Marcus599 | Posted: 01-03-2022



Answer 2

As an aside: When using functions instead of methods, you could also use functools.partial:

import functools

def foo(arg, **kwargs):
    ...

bar = functools.partial(foo, "argvalue")

The last line will define a function "bar" that, when called, will call foo with the first argument set to "argvalue" and all other functions just passed on:

bar(5, myarg="value")

will call

foo("argvalue", 5, myarg="value")

Unfortunately that will not work with methods.

Answered by: Agata608 | Posted: 01-03-2022



Answer 3

Some experimentation and I figured this one out:

def methodA(arg, **kwargs): methodB("argvalue", **kwargs)

Seems obvious now...

Answered by: Emily930 | Posted: 01-03-2022



Similar questions

Passing functions which have multiple return values as arguments in Python

So, Python functions can return multiple values. It struck me that it would be convenient (though a bit less readable) if the following were possible. a = [[1,2],[3,4]] def cord(): return 1, 1 def printa(y,x): print a[y][x] printa(cord()) ...but it's not. I'm aware that you can do the same thing by dumping both return values into temporary variables, but it doesn't seem as elega...


Passing functions with arguments to another function in Python?

This question already has answers here:


python - Soaplib functions with default arguments

I have to write soaplib method, that has many arguments. The idea is that the user should able able to choose, which arguments he wants to provide. Is that even possible? I know it is possible in python generally, but there is an error, when i try to set it up like normal python method with default arguments.


python - How to get a functions arguments and values from outside the function?

I have searched a little bit to try to figure this one out but didn't get a solution that I was exactly looking for. This is my use case: I would like to evaluate expressions from a functions/methods doc-string against the f/m's parameters and values, but from outside the function (when being called but outside execution of the function I can't statically change the source code I am evaluatin...


events - Python Button Bind two functions and send arguments with the binding

I am somewhat new to python still, and am working on a project including a GUI. I using with Tkinter and buttons a lot, and am curious if there was a way to run a function with a bind because I want one thing to happen when it is presses and something else when it is released. s = str(x+1) + ":" + str(y+1) img = ImageTk.PhotoImage(Image.open('button.png')) b = Tkinter.Button(field_hi...


python - How to call a two functions with unequal arguments using same function call?

I have two definitions say, file x.py: class x: def p(self, a, b) : # ... file y.py: class y: def p(self, a, b, c) : # [...] Now I am calling these functions from another file. Command line inputs having the file names (in this case x and y) are passed in cust variable. file z.py: from ...


python - Calling functions with arguments on "command" and "bind"

i want to point, that i am learning python since short time. The question is going be to beginner one. I need to add command to menu at top of program, which would call function "color_picker("red"). kolory.add_command(label="Czerwony", command=color_picker('red')) When i am using that, its somehow wrong, cuz its called once the program started, its not waiting for me to click the menu butto...


python - Please explain why these two builtin functions behave different when passed in keyword arguments

Consider these different behaviour:: >> def minus(a, b): >> return a - b >> minus(**dict(b=2, a=1)) -1 >> int(**dict(base=2, x='100')) 4 >> import operator >> operator.sub.__doc__ 'sub(a, b) -- Same as a - b.' >> operator.sub(**dict(b=2, a=1)) TypeError: sub() takes no keyword arguments Why does operator.sub behave differently fr...


Python: How to pass functions to another function as arguments?

I have 2 custom functions: f(), g() I want to pass all months to them, and pass them another function as follows: x(f("Jan"), g("Jan"), f("Feb"), g("Feb"), f("Mar"), g("Mar"), ...) How is it done in short way? Best Regards


mocking - How to mock functions in Python in order to change default keyword arguments

I'm using the mock library and unittest2 in order to test different aspects of my software project. At the moment I have the following question: is it possible to mock a function so that the default keyword argument is different, but the functionality remains? Say I have the following code class C(): def fun(self, bool_arg = True): if bool_arg: return True else return F...


storing unbound python functions in a class object

I'm trying to do the following in python: In a file called foo.py: # simple function that does something: def myFunction(a,b,c): print "call to myFunction:",a,b,c # class used to store some data: class data: fn = None # assign function to the class for storage. data.fn = myFunction And then in a file called bar.py: import foo d = foo.data d.fn(1,2,3)


python - Running unit tests on nested functions

I come from the Java world, where you can hide variables and functions and then run unit tests against them using reflection. I have used nested functions to hide implementation details of my classes so that only the public API is visible. I am trying to write unit tests against these nested functions to make sure that I don't break them as I develop. I have tried calling one of the nested functions like:


python - Porting MATLAB functions to Scilab. How do I use symbolic?

I'm porting some MATLAB functions to Scilab. The cool thing is that there is a conversion toolbox that make things very easy. The problem is I did not find the counterpart to...


python - Parsing Functions

I'm making a script parser in python and I'm a little stuck. I am not quite sure how to parse a line for all its functions (or even just one function at a time) and then search for a function with that name, and if it exists, execute that function short of writing a massive list if elif else block.... EDIT This is for my own scripting language that i'm making. its nothing very complex, but i have a...


python - How to call java objects and functions from CPython?

I have a python program, which runs on the CPython implementation, and inside it I must call a function defined in a java program. How can I do this? It would be nice to be able to use some java objects too. Jython is not an option. I must run the python part in CPython.


Passing functions which have multiple return values as arguments in Python

So, Python functions can return multiple values. It struck me that it would be convenient (though a bit less readable) if the following were possible. a = [[1,2],[3,4]] def cord(): return 1, 1 def printa(y,x): print a[y][x] printa(cord()) ...but it's not. I'm aware that you can do the same thing by dumping both return values into temporary variables, but it doesn't seem as elega...


Passing self to class functions in Python

This question already has answers here:


c# - Import python functions into a .NET language?

I am a C# .NET programmer and am learning Python. I have downloaded IronPython, and know that it can call into .NET libraries. I'm wondering whether there is a way to do the reverse, that is to call into some existing "classic" Python libraries in my C# code, maybe using .NET Interop. I'd like to be able to access functions in libraries such as pygame.


Python Hash Functions

What is a good way of hashing a hierarchy (similar to a file structure) in python? I could convert the whole hierarchy into a dotted string and then hash that, but is there a better (or more efficient) way of doing this without going back and forth all the time? An example of a structure I might want to hash is: a -> b1 -> c -> 1 -> d a -> b2 -> c -> 2 -> d a -> c ...


Passing functions with arguments to another function in Python?

This question already has answers here:






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top