Getting local dictionary for scope only in Python
I continue in this situation when I want to use a dictionary very similar to what "locals" returns, but it only contains variables in the limited scope of the function. Is there a way to do this in python?
A little more about why I want to do this: I am playing with Django and when I go to give my template context I am forced to either manually make a dictionary (in violation of DRY principles) or pass it to locals (), which contains a lot more entries then necessary (wasteful). Is there something I don't see in django that would make it easier to have a python-level solution?
To clarify:
So, the case where I clicked multiple times is where I am:
@render_to('my_template.html')
def myview(request):
var1 = #blahblah
var2 = #...
# do stuff with vars
return {'var1': val1,'var2':val2}
So instead of repeating these variables and naming conventions, I'll do the following:
@render_to('my_template.html')
def myview(request):
var1 = #blahblah
var2 = #...
# do stuff with vars
return locals()
Which I find to be cleaner, but I know its sloppy, since there are about 30 more entries () in locals (), then I really need it.
a source to share
I'm not sure if I agree that making a dictionary is a DRY violation, but if you really don't want to repeat anything, you can simply define a "context" dictionary at the top of the view and use dictionary keys instead of variables throughout the view.
def my_view(request):
context = {}
context['items'] = Item.objects.all()
context['anothervalue'] = context['items'][2].name
return render_to_response('template.html', context)
a source to share
How does a dictionary with violation DRY go? Django is all about DRY , so I doubt its standard behavior would outright violate it. Anyway, however, I'm using a modified version of django-annoying to simplify the whole thing:
@render_to('my_template.html')
def myview(request):
# figure stuff out...
return {'var1':'val1','var2','val2'}
The decorator render_to
will take care of the context of the request and all that good stuff. Works well.
If that doesn't help, I suggest rephrasing your question. Whatever you want to mess up with locals()
is rarely especially necessary in this Django situation.
a source to share
You say that you don't like using locals () because it is "wasteful". Disgusting what? I believe the dictionary it returns already exists, it just gives you a reference to it. And even if it has to create a dictionary, this is one of the most optimized operations in Python, so don't worry about it.
You should focus on the structure of the code that best reflects your intent, with the least potential for error. The waste you worry about is nothing to worry about.
a source to share
While I agree with many other respondents that passing locals()
or a fully specified dict {'var1':var1, 'var2': var2}
is likely to be OK if you specifically want a "subset" of the dict, for example locals()
, which is far from difficult, for example:
loc = locals()
return dict((k,loc[k]) for k in 'var1 var2'.split())
a source to share