[Python-Dev] summary of transitioning from % to {} formatting (original) (raw)
Antoine Pitrou solipsis at pitrou.net
Sat Oct 3 18:23:24 CEST 2009
- Previous message: [Python-Dev] summary of transitioning from % to {} formatting
- Next message: [Python-Dev] summary of transitioning from % to {} formatting
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Define "fails":
>>> "{a} {b} c" % {'a':12} '{a} {b} c' That didn't fail...
Ah, my bad. I had completely overlooked that formatting was laxist when faced with unused named parameters.
Then we need something smarter, like counting the number of unescaped "%" characters, the number of pairs of braces, and try first the formatting style expecting the greatest number of parameters. It can either be done cheaply in Python, or more rigourously by providing an extra API to the existing C parsing routines.
This might look fragile, but we should not forget that we are talking about uses - logging, etc. - which will most of time involve very simple format strings, such that making an automatic decision is not too difficult. In the more undecideable cases, the heuristic might decide to raise an exception and require the developer to specify the formatting style explicitly (by adding e.g. style='%' or style='{}' to the method call).
Regards
Antoine.
- Previous message: [Python-Dev] summary of transitioning from % to {} formatting
- Next message: [Python-Dev] summary of transitioning from % to {} formatting
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]