Is it possible to have a class be formatted in its own specific way with the %
operator, in Python? I am interested in a case where the format string would be something like %3z
(not simply %s
or %r
, which are handled by the __str__()
and __repr__()
methods). Said differently: Python 2.6+ allows classes to define a __format__()
method: is there an equivalent for the %
operator?
I tried to define the __rmod()__
method, hoping that str.__mod__()
would return NotImplemented
and that __rmod()__
would be called, but "%3z" % …
returns ValueError: unsupported format character 'z'
instead…