Just use the repr()
of self.a
:
return "Foo(a={self.a!r})".format(self=self)
The !r
tells .format()
to call repr()
on self.a
, instead of calling __format__()
on the value, see Format String syntax:
>>> '{!r}'.format(0)
'0'
>>> '{!r}'.format('foo')
"'foo'"
Demo:
>>> Foo(0)
Foo(a=0)
>>> Foo('foo')
Foo(a='foo')
>>> Foo(None)
Foo(a=None)