kj wrote:
In <mailman.113.1242254593.8015.python-l...@python.org> Terry Reedy 
<tjre...@udel.edu> writes:

kj wrote:
Suppose I have the following:

def foo(x=None, y=None, z=None):
    d = {"x": x, "y": y, "z": z}
    return bar(d)

I.e. foo takes a whole bunch of named arguments and ends up calling
a function bar that takes a single dictionary as argument, and this
dictionary has the same keys as in foo's signature, so to speak.

Is there some builtin variable that would be the same as the variable
d, and would thus obviate the need to explicitly bind d?

Use the built-in function locals()
def f(a,b):
        x=locals()
        print(x)

f(1,2)
{'a': 1, 'b': 2}

That's *exactly* what I was looking for.  Thanks!

kynn


You already had a better answer from Chris Rebert:

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

kwargs at this point is exactly a dictionary of the named arguments to foo.

Because if you try to do anything in this function, you'll probably be adding more local variables. And then they'd be passed to bar as well.


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to