4 ms·
Having a choice on when to use the existing string formatting methods is a feature. I tend to use ‘%’ a lot for it’s compactness and f-strings most more rarely
by CrLf 6y ago
Having a choice on when to use the existing string formatting methods is a feature. I tend to use ‘%’ a lot for it’s compactness and f-strings most more rarely (admittedly there doesn’t seem to be much reason to use .format these days).
In general I don’t understand the need to hard-standardize code. Making subtle decisions depending on context is an important part of programming, please let humans decide and don’t use these types of tools in commit hooks...
- afarrell 6y ago> Making subtle decisions depending on context is an important part of programming. Yes, and so is deciding what context matters across a whole team or team-of-teams.
- BerislavLopac 6y agoOne case where .format comes in handy: your_dict = { "some": "awesome", "from": "out of", "dictionary": "mapping", } "insert {some} values {from} a {dictionary}".format(**your_dict)
- OskarS 6y agoThat is a very neat trick indeed, I'll have to remember this one!
- CrLf 6y agoThis can also be achieved using '%': your_dict = { "some": "awesome", "from": "out of", "dictionary": "mapping", } "insert %(some)s values %(from)s a %(dictionary)s" % your_dict WRT .format, '%' is safer to use with untrusted (well, semi-trusted) format strings, whereas .format can lead to code injection (in more insidious ways than with f-strings).
- ericvsmith 6y agoYou'd be better off using "...".format_map(your_dict). This case is exactly why it exists.