[Python-ideas] String interpolation again. (original) (raw)
Eric Smith eric at trueblade.com
Fri Jul 23 21:38:56 CEST 2010
- Previous message: [Python-ideas] String interpolation again.
- Next message: [Python-ideas] [Python-Dev] Set the namespace free!
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 7/23/10 3:26 PM, Dag Odenhall wrote:
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 = '...{shortdescriptivename}...' text = format.format( shortdescriptivename=longcumbersomevariablename, othername=othername, ...) Don't be afraid of more lines of code if it helps readability. You could also build a context dict: context = {} context['name'] = complicatedcodetogetname() ... text = '...{name}...'.format(**context)
Or use: text = '...{context.name}...'.format(context=context)
This is a great trick when "context" is "self".
-- Eric.
- Previous message: [Python-ideas] String interpolation again.
- Next message: [Python-ideas] [Python-Dev] Set the namespace free!
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]