Re:Improving your (Python) code with modern idioms
by andolanra on Tuesday May 08, 2012 @07:12AM
(#3943085)
The new-style formatting allows that as well:
It also allows for more complex expressions inside of format specifiers, so if you need to grab data out of an object, you can do something like:
which is verbose and contrived here, but could be immensely useful in cases like
[ Parent
| on HN ]
Re:Improving your (Python) code with modern idioms
by 286c8cb04bda on Tuesday May 08, 2012 @10:38AM
(#3943814)
How do you handle it when you need to convert, e.g., "American woman" to "femme américaine"?
Re:Improving your (Python) code with modern idioms
by andolanra on Tuesday May 08, 2012 @12:26PM
(#3944418)
You use something else. It's still just a format string, not a whole localization solution. Named arguments means it might be easier to hack together something like
templates = {'en': '{adj} {noun}', 'fr': '{noun} {adj}'}
print(templates['fr'].format(noun='fromage', adj='délicieux')
but that's still a hack, and doesn't even come close to addressing cases like Chinese's "{adj} {counter_word[noun]} {noun}" or gender concord or any of the myriad other things you come across in practice.Edit: used 'positional' instead of 'named'.