Python __format__: Detecting an empty format code

Python __format__: Detecting an empty format code

Defining __format__ in a Python class? Check if the second argument is ”, meaning no format code:

def __format__(self, format):
    if format == '':
        return "(No format code)"
    else:
        return f'>> {self.x} <<'