Вопрос

I basically want to expand the current scope as you would a dictionary when calling a function.

I remember seeing something about this somewhere but I cannot remember where or how to do it.

Here is a simple example

def bar(a, b, c, d, e, f):
    pass

def foo(a, b, c, d, e, f):
    # Instead of doing this
    bar(a, b, c, d, e, f)
    # or
    bar(a=a, b=b, c=c, d=d, e=e, f=f)
    # I'd like to do this
    bar(**local_scope)

Am I imagining things or can this really be done?

Это было полезно?

Решение

You can use locals() (or globals() depending on what you need), which returns a dictionary mapping variable names to values.

bar(**locals())

Другие советы

if foo was written like this

def foo(**kwargs):
    bar(**kwargs)

Other than that the other two examples you posted are better, expanding all locals is a bad idea.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top