Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In method call args, how to override keyword argument of unpacked dict?

I'm trying to use template keyword args for convenience in a function call (via dict and keyword arguments) whilst being able to override some arguments.

For example, if we start with a module mymod containing the line template_kvps = {'a': 1, 'b': 2}

I can just:

import mymod

def func(**kwargs):
    pass

func(**mymod.template_kvps)

then I can access my template_kvps within func(). But I want to be able to pass a different value for a with minimal overhead.

All I can think of is to alter the dictionary before the function call: kvps = {**template_kvps, 'a': 3}; func(**kvps), but that's twice the number of lines and I'm using this function several times in each of around 1000 test scripts.

I'd ideally like to redefine func so that I can do sth like func(**mymod.template_kvps, a=3) but as it is, Python errors with something about repeated parameters.

btw I'm happy to consider changing the format of the template_kvps.

EDIT (will move to answer at some point) I could use a wrapper method instead

def func_template(a=1, b=2):
    func(a, b)

func_template(a=3)
like image 706
joel Avatar asked Jun 27 '17 15:06

joel


People also ask

How do you pass keyword arguments in Python?

Python has *args which allow us to pass the variable number of non keyword arguments to function. In the function, we should use an asterisk * before the parameter name to pass variable length arguments.

How do you beat Kwargs argument?

Kwargs allow you to pass keyword arguments to a function. They are used when you are not sure of the number of keyword arguments that will be passed in the function. Kwargs can be used for unpacking dictionary key, value pairs. This is done using the double asterisk notation ( ** ).

Can we pass keyword arguments in any order?

A keyword argument is a name-value pair that is passed to the function. Here are some of the advantages: If the values passed with positional arguments are wrong, you will get an error or unexpected behavior. Keyword arguments can be passed in any order.

How do you pass Kwargs through a function?

Use the Python **kwargs parameter to allow the function to accept a variable number of keyword arguments. Inside the function, the kwargs argument is a dictionary that contains all keyword arguments as its name-value pairs. Precede double stars ( ** ) to a dictionary argument to pass it to **kwargs parameter.


1 Answers

You can use the built-in dict type for that purpose. It accepts another dict as argument and additional key-value pairs as keyword arguments (which have precedence over the values in the other dict).

Thus you can create an updated dictionary via dict(template_vars, a=1).

You can unfold this dict as keyword arguments: func(**dict(...)).

Like that there is no need to change the signature of your function and you can update/add as many key-value pairs as you want.

like image 165
a_guest Avatar answered Sep 27 '22 17:09

a_guest