[Python-Dev] bpo-34595: How to format a type name? (original) (raw)
Eric V. Smith eric at trueblade.com
Thu Sep 13 20:24:00 EDT 2018
- Previous message (by thread): [Python-Dev] bpo-34595: How to format a type name?
- Next message (by thread): [Python-Dev] bpo-34595: How to format a type name?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 9/13/2018 8:04 PM, Victor Stinner wrote:
Le ven. 14 sept. 2018 à 00:09, Eric V. Smith <eric at trueblade.com> a écrit :
f'{type(obj)}' becomes type(obj).format(''), so you can return something other than str or repr does. It's only by convention that an object's format returns str: it need not do so. What's New in Python 3.7 contains:
object.format(x, '') is now equivalent to str(x) rather than format(str(self), ''). (Contributed by Serhiy Storchaka in bpo-28974.) https://bugs.python.org/issue28974 Oh, I didn't know that a type is free to change this behavior: return something different than str(obj) if the format spec is an empty string.
True! That issue was specific to object.format, not any other classes implementation of format.
So are you suggesting to change type(obj).format('') to return the fully qualified name instead of repr(type)?
I'm not suggesting it, I'm saying it's possible. It indeed might be the most useful behavior.
So "%s" % type(obj) would use repr(), but "{}".format(type(obj)) and f"{type(obj)}" would return the fully qualified name?
"%s" % type(obj) would use str(), not repr.
You could either:
- keep with convention and have type(obj).format('') return type(obj).str(), while type(obj).format('#') (or what other char you want to use) return the qualname; or
- just have type(obj).format('') return the qualname, if that's the more useful behavior.
Eric
- Previous message (by thread): [Python-Dev] bpo-34595: How to format a type name?
- Next message (by thread): [Python-Dev] bpo-34595: How to format a type name?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]