views:

268

answers:

6

In the work I do, I often have parameters that I need to group into subsets for convenience:

d1 = {'x':1,'y':2}
d2 = {'a':3,'b':4}

I do this by passing in multiple dictionaries. Most of the time I use the passed dictionary directly, i.e.:

def f(d1,d2):
    for k in d1:
        blah( d1[k] )

In some functions I need to access the variables directly, and things become cumbersome; I really want those variables in the local name space. I want to be able to do something like:

def f(d1,d2)
    locals().update(d1)
    blah(x)
    blah(y)

but the updates to the dictionary that locals() returns aren't guaranteed to actually update the namespace.

Here's the obvious manual way:

def f(d1,d2):
    x,y,a,b = d1['x'],d1['y'],d2['a'],d2['b']
    blah(x)
    return {'x':x,'y':y}, {'a':a,'b':b}

This results in three repetitions of the parameter list per function. This can be automated with a decorator:

def unpack_and_repack(f):
    def f_new(d1, d2):
        x,y,a,b = f(d1['x'],d1['y'],d2['a'],d3['b'])
        return {'x':x,'y':y}, {'a':a,'b':b}
    return f_new
@unpack
def f(x,y,a,b):
    blah(x)
    blah(y)
    return x,y,a,b

This results in three repetitions for the decorator, plus two per function, so it's better if you have a lot of functions.

Is there a better way? Maybe something using eval? Thanks!

+1  A: 

If you like d.variable syntax better than d['variable'], you can wrap the dictionary in an almost trivial "bunch" object such as this:

class Bunch:
    def __init__(self, **kw):
        self.__dict__.update(kw)

It doesn't exactly bring dictionary contents into the local namespace, but comes close if you use short names for the objects.

Jouni K. Seppänen
+1  A: 

I do not think you can get any more convenience for dict unpacking in Python. So, here comes the obligatory "if that hurts, don't do that" answer.

Mapping item access IS more cumbersome than attribute access in Python, so maybe you should pass instances of user-defined classes instead of dicts.

ddaa
+1  A: 

I think the common wisdom is "don't use the inspect module in production code", and I mostly agree with that. As such, I think it is a bad idea to do the following in production code. But, if you're working on a python that supports frames (like CPython), this should work:

>>> def framelocals():
...    return inspect.currentframe(1).f_locals
... 
>>> def foo(ns):
...    framelocals().update(ns)
...    print locals()
... 
>>> foo({'bar': 17})
{'ns': {'bar': 17}, 'bar': 17}

It just grabs the actual dict out of the caller's frame, which when called inside a function body, should be the function's name space. I don't know if there is or isn't a situation when using CPython when locals() doesn't just do this anyway; the warning in the documentation might be to say "the effects of modifying the dict returned by locals() are python implementation dependent". Thus, while it works to modify that dict in CPython, it may not in another implementation.

UPDATE: This method doesn't actually work.

>>> def flup(ns):
...    framelocals().update(ns)
...    print locals()
...    print bar
... 
>>> flup({'bar': 17})
{'ns': {'bar': 17}, 'bar': 17}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in flup
NameError: global name 'bar' is not defined

As ddaa suggested, there's some deeper magic in the function compilation which makes note of the local variables. Thus you can update the dict, but you can't see the update with normal local namespace lookup.

Matt Anderson
Although that might technically work, this is a terrible idea for readability. Also I suspect it will not actually work correctly because references to bar will not be compiled as local variable access in foo.
ddaa
To bypass that particular bit of "deep magic" in variable lookup, add something like exec "" to the function to keep the Python compiler from optimizing the function. Then I think even locals().update will work.Not that I would recommend this technique.
Jouni K. Seppänen
+2  A: 

This is similar to your decorator idea, but it is a bit more general, as it allows you to pass an arbitrary number of dicts to foo, and the decorator does not have to know anything about the keys in the dicts or the order of the arguments when calling the underlying foo function.

#!/usr/bin/env python
d1 = {'x':1,'y':2}
d2 = {'a':3,'b':4}

def unpack_dicts(f):
    def f_new(*dicts):
        new_dict={}
        for d in dicts:
            new_dict.update(d)
        return f(**new_dict)
    return f_new

@unpack_dicts
def foo(x,y,a,b):
    print x,y,a,b

foo(d1,d2)
# 1 2 3 4
unutbu
That's an improvement... But you still would have to manually repack the return values of the function. Too bad there aren't named return parameters... If I were qualified to implement this, I'd consider writing a PEP. Thanks!
Drew Wagner
+1  A: 

You can always pass a dictionary as an argument to a function. For instance,

dict = {'a':1, 'b':2}
def myFunc( a=0,b=0,c=0 ):
  print a,b,c
myFunc(**dict)
Bear
Thanks, but I'm aware of that. I'm trying to abstract away the argument lists for the purpose of generalizing the code.
Drew Wagner
+1  A: 

Assuming all keys in your dictionary qualify to be identifiers, You can simply do this:

adict = { 'x' : 'I am x', 'y' : ' I am y' }

for key in adict.keys(): exec(key + " = adict['" + key + "']")

blah(x) blah(y)

Thava