Python etc / format

Published: 08 September 2020, 18:00

There is a built-in function format that basically just calls __format__ method of the passed argument type with passed spec. It is used in str.format as well.

class A:
    def __format__(self, spec):
        return spec

format(A(), 'oh hi mark')
# 'oh hi mark'

'{:oh hi mark}'.format(A())
# 'oh hi mark'