Need help with custom string formatter

Cameron Simpson cs at cskk.id.au
Fri Oct 21 19:04:18 EDT 2022


On 21Oct2022 16:55, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>Robert Latest <boblatest at yahoo.com> writes:
>>>return super().format_field( value, format_string )
>>Why do you prefer super().format_field() over plain format()? The doc says:
>>"format_field() simply calls format()." So I figured I might do the same.
>
>  I am not aware of any reason not to call "format" directly.

Stefan's code implements it's own format_field and falls back to the 
original format_field(). That's standard subclassing practice, and worth 
doing reflexively more of the time - it avoids _knowing_ that 
format_field() just calls format().

So I'd take Stefan's statement above to imply that calling format() 
directly should work.

My own habit would be to stick with the original, if only for semantic 
reasons. Supposing you switched from subclassing Formatter to some other 
class which itself subclasses Formatter. (Yes, that is probably 
something you will never do.) Bypassing the call to 
super().format_field(...) prevents shoehorning some custom format_fields 
from the changed superclass. Which you'd then need to debug.

That's all.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list