Skip to content

Instantly share code, notes, and snippets.

@MichaelBlume
Created May 6, 2011 21:18
Show Gist options
  • Save MichaelBlume/959800 to your computer and use it in GitHub Desktop.
Save MichaelBlume/959800 to your computer and use it in GitHub Desktop.
Localdict -- reduce your typing by half when constructing a dictionary literal from objects in the local context.
'''Localdict -- reduce your typing by half when constructing a dictionary
literal from objects in the local context.
params = {'foo': foo, 'bar': bar, 'baz': baz}
can be replaced ith
params = localdict('foo', 'bar', 'baz')
'''
import inspect
def localdict(*args):
one_up_locals = inspect.stack()[1][0].f_locals
return dict([(k, one_up_locals[k]) for k in args])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment