Recently, I found the feature ''.formatvery useful, as it can significantly improve readability compared to formatting %. Trying to achieve simple string formatting:
data = {'year':2012, 'month':'april', 'location': 'q2dm1'}
year = 2012
month = 'april'
location = 'q2dm1'
a = "year: {year}, month: {month}, location: {location}"
print a.format(data)
print a.format(year=year, month=month, location=location)
print a.format(year, month, location)
While the first two prints make the format, as I expect (yes, something=somethingit looks ugly, but just for example), the latter will pick up KeyError: 'year'. Is there a trick in python to create a dictionary so that it automatically populates keys and values, e.g. somefunc(year, month, location)outputs {'year':year, 'month': month, 'location': location}?
I am new to python and cannot find any information on this topic, however such a trick would improve and shorten my current code.
Thanks in advance and apologize for my English.