Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
It is possible to create a new formatter class by subclassing string.Formatter class and then to use it like myformatter.format("{foo:customformat}", foo=foo). It's not super-convenient though, and...
#2: Post edited
- It is possible to create a new formatter class by subclassing `string.Formatter` class and then to use it like `myformatter.format("{foo:customformat}", foo=foo)`. It's not super-convenient though, and above all, it fails miserably with f-strings, i.e. `f"{foo:customformat}"` won't use my custom formatter class.
I also tried to subclass `str` and override the `.format()` method, but it still won't cut for f-strings.
- It is possible to create a new formatter class by subclassing `string.Formatter` class and then to use it like `myformatter.format("{foo:customformat}", foo=foo)`. It's not super-convenient though, and above all, it fails miserably with f-strings, i.e. `f"{foo:customformat}"` won't use my custom formatter class.
- I also tried to subclass `str` and override the `.format()` method, but it still won't cut for f-strings.
#1: Initial revision
How to override default string formatter?
It is possible to create a new formatter class by subclassing `string.Formatter` class and then to use it like `myformatter.format("{foo:customformat}", foo=foo)`. It's not super-convenient though, and above all, it fails miserably with f-strings, i.e. `f"{foo:customformat}"` won't use my custom formatter class. I also tried to subclass `str` and override the `.format()` method, but it still won't cut for f-strings.