Elegant way to unpack limited dict values into local variables in Python
You can do something like
foo, bar = map(d.get, ('foo', 'bar'))
or
foo, bar = itemgetter('foo', 'bar')(d)
This may save some typing, but essentially is the same as what you are doing (which is a good thing).
Somewhat horrible, but:
globals().update((k, v) for k, v in d.iteritems() if k in ['foo', 'bar'])
Note, that while this is possible - it's something you don't really want to be doing as you'll be polluting a namespace that should just be left inside the dict
itself...
Well, if you know the names ahead of time, you can just do as you suggest.
If you don't know them ahead of time, then stick with using the dict - that's what they're for.
If you insist, an alternative would be:
varobj = object()
for k,v in d.iteritems(): setattr(varobj,k,v)
After which, keys will be variables on varobj
.