24 Jul
2010
24 Jul
'10
12:56 a.m.
In my code, '{bar}' and 'bar=bar' seems too verbose. Your code is simple when the literal is short, but it is difficult to check which variable is inserted where when string is long.
format = '...{short_descriptive_name}...' text = format.format( short_descriptive_name=long_cumbersome_variable_name, other_name=other_name, ...) Don't be afraid of more lines of code if it helps readability. You could also build a context dict: context = {} context['name'] = complicated_code_to_get_name() ... text = '...{name}...'.format(**context) Of course personally I want a where/given clause, but that's a different thread. ;)