[Python-ideas] Dunder method to make object str-like

Chris Angelico rosuav at gmail.com
Thu Apr 7 09:53:41 EDT 2016

On Thu, Apr 7, 2016 at 11:46 PM, Émanuel Barry <vgr255 at live.ca> wrote:
>> 4) Should there be a format string "coerce to str"? "{}".format(x) is
> equivalent to str(x), but it might be nice to be able to assert that
> something's stringish already.
> __index__ is meant to return the same thing as __int__, and I think the same
> restrictions should apply here - we have an object pretending to be a
> string, so there should *not* be a difference between use_as_string(obj) and
> use_as_string(str(obj)) - in the same sense that there should not be a
> difference between use_as_int(obj) and use_as_int(int(obj)).

Absolutely agreed; however, I was thinking of the same restriction:
"{!must_be_str}".format(x) would raise an exception if use_as_str(x)
raises. But if it succeeds, yes, it's the same as simple str()


More information about the Python-ideas mailing list