3 ms·
This is a simple example that does seem more readable (though it also would be if you explicitly added the value=value), but I think the concerns people have ar
by inclemnet 11y ago
This is a simple example that does seem more readable (though it also would be if you explicitly added the value=value), but I think the concerns people have are about more complex examples, and the fact that readability is also impacted if allowing arbitrary code within strings themselves.
If you don't like using the empty {} (which seems fair enough), you can already use a couple of slightly more verbose but more explicit options:
"{value} of very long text here...".format(**locals)
or (more verbose with many keys)
"{value} of very long text here...".format(value=value)
I tend to prefer the latter even with multiple keys, but also think the former is better as it makes it explicit that the string is populated with the locals rather than implicitly doing it to all strings.
- baq 11y ago.format(locals()) is a hack; locals() isn't supported on some python implementations.
- inclemnet 11y agoThe documentation at https://docs.python.org/3/library/functions.html#locals https://docs.python.org/3/library/functions.html#locals doesn't indicate that locals is not part of the language - where is it documented that using it is a hack and that python implementations need not support it?
- wyldfire 11y agoIt is on PyPy and CPython. Which ones don't? And when they don't support it, how do they behave? Return an empty dict or give an error?