Steve Holden wrote:
Nick Coghlan wrote:
To elaborate on this a bit (and handwaving a lot of important details out of the way) do you mean something like the following for the builtin format?:
def format(obj, fmt_spec=None): if fmt_spec is None: fmt_spec='' result = obj.__format__(fmt_spec) if isinstance(fmt_spec, unicode): if isinstance(result, str): result = unicode(result) return result
Isn't unicode idempotent? Couldn't
if isinstance(result, str): result = unicode(result)
avoid repeating in Python a test already made in C by re-spelling it as
result = unicode(result)
or have you hand-waved away important details that mean the test really is required?
This code is written in C. It already has a check to verify that the return from __format__ is either str or unicode, and another check that fmt_spec is str or unicode. So doing the conversion only if result is str and fmt_spec is unicode would be a cheap decision.
Good catch, though. I wouldn't have thought of it, and there are parts that are written in Python, so maybe I can leverage this elsewhere. Thanks!
Eric.