3

I have a function that takes in kwargs. I want to use the kwargs parameters by name without having to declare each explicitly as either a parameter or variable. Is there a way to use my_var_key by passing in kwargs without having to specifically define it in the function call, or is the only way to use kwargs[ "my_var_key" ]?

E.g. I want something like

def func(**kwargs):
    print(my_var_key)

as opposed to

def func(my_var_key, **kwargs):
    print(my_var_key)

or

def func(**kwargs):
    print(kwargs[ "my_var_key" ])

I'm okay with it breaking if the key doesn't exist.

Elliptica
  • 3,928
  • 3
  • 37
  • 68
  • Why don't you just pass in a dictionary and work on that dictionary? – AmagicalFishy Feb 14 '19 at 01:08
  • You possibly could do something by wrapping your code in `try ... except NameError` to catch a `NameError`, [*parse* the `NameError` error message to extract the variable name](https://stackoverflow.com/questions/2270795/getting-the-name-which-is-not-defined-from-nameerror-in-python), and then look up the extracted name in `kwargs` ... but that's very gross and brittle, and I'm not really sure what that would buy you. Is there any motivation for this other than to reduce some typing? – jamesdlin Feb 14 '19 at 01:08
  • @jamesdlin For motivation, let's say (this is extreme for illustration) you had five functions where each takes in 100 arguments. Say that most of those arguments are going to be the same keyword but some arguments will exist that aren't in the others. The functions also use them all very differently. I don't want to have to explicitly declare 100 variables five times, but there's too any unique parameters to make doing a common subset worthwhile either. The best is to have a kwargs dict of all the common plus unique parameters, defaulted to empty values, and pass that to each function. – Elliptica Feb 14 '19 at 01:47
  • I would probably try to use a decorator to reduce the boilerplate. – jamesdlin Feb 14 '19 at 02:14

2 Answers2

2

No, there's no practical way to get the syntax you want.

One part of Python's design philosophy is that "explicit is better than implicit", so there are not many situations where names will get added to your namespace without your input. If you want my_var_key to be put into your function's namespace when it's passed as an argument, you should just name it in the def statement. If you want it to only be passable as a keyword argument (not a positional one), you can put a * in the argument list:

def func(*, my_var_key): # arg can only be passed as a keyword: func(my_var_key=1)
   ...
Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

Don't do this. It makes your code harder to read.

But if you insist, try modify your globals() scope:

def func(**kwargs):
    for key, val in kwargs.items():
        globals()[key] = val
    print(globals())
    print(my_var_key)

func(my_var_key='foobar')

and the output is:

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10f058f28>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'test.py', '__cached__': None, 'func': <function func at 0x10ef52268>, 'my_var_key': 'foobar'}
foobar

This will surely pollute your global namespace (and you cannot use locals() because the interpreter confuses). Again, don't do this.

adrtam
  • 6,991
  • 2
  • 12
  • 27