I keep ending up at this situation where I want to use a dictionary very much like the one 'locals' gives back, but that only contains the variables in the limited scope of the function. Is there a way to do this in python?
A bit more about why I want to do this: I'm playing with Django and when I go to give my templates context, I am forced either to either manually make a dictionary (In violation with DRY principles) or pass in locals() which contains far more entries then are needed (wasteful). Is there perhaps something I'm missing with django which would alleviate the need of a python level solution?
To Clarify:
So, the case that I've hit repeatedly is where I have:
@render_to('my_template.html')
def myview(request):
var1 = #blahblah
var2 = #...
# do stuff with vars
return {'var1': val1,'var2':val2}
So instead of repeating those variables and naming conventions, I'll do:
@render_to('my_template.html')
def myview(request):
var1 = #blahblah
var2 = #...
# do stuff with vars
return locals()
Which I find cleaner, but I know its kind of sloppy since there are about 30 more entries in locals() then I actually need.