This is a hack. It will not work on all Python implementations distributions (in particular, those that do not have traceback.extract_stack.)
import traceback def make_dict(*expr): (filename,line_number,function_name,text)=traceback.extract_stack()[-2] begin=text.find('make_dict(')+len('make_dict(') end=text.find(')',begin) text=text[begintext=[name.strip() for name in text[begin:end].split(',')] return dict(zip(text,expr)) bar=True foo=False print(make_dict(bar,foo)) # {'foo': False, 'bar': True} Note that this hack is fragile:
make_dict(bar, foo) (calling make_dict on 2 lines) will not work.
Instead of trying to generate the dict out of the values values foo and bar, it would be much more Pythonic to generate the dict out of the string variable namesvariable names 'foo' and 'bar':
dict([(name,locals()[name]) for name in ('foo','bar')])