[Python-Dev] PEP 498 (interpolated f-string) tweak (original) (raw)
Guido van Rossum guido at python.org
Sat Sep 19 18:25:32 CEST 2015
- Previous message (by thread): [Python-Dev] PEP 498 (interpolated f-string) tweak
- Next message (by thread): [Python-Dev] PEP 498 (interpolated f-string) tweak
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Good catch, Eric.
For those who wonder what the difference is or why it matters, it's what we do for all dunder operators: the magic method is looked up on the class, not on the instance. This means you can't e.g. override + on a per-instance basis by having an instance variable named 'add' pointing to some function. That's a rare use case, it's pretty obfuscated, it would slow down everything, and if you really need that pattern, there are other ways to do it (you could have a regular instance method add on the class that looks for an instance variable, e.g. named _add, and call it if it exists -- otherwise call super().add).
--Guido
On Sat, Sep 19, 2015 at 4:03 AM, Eric V. Smith <eric at trueblade.com> wrote:
While finishing up the implementation of PEP 498, I realized that the PEP has an error. It says that this code:
f'abc{expr1:spec1}{expr2!r:spec2}def{expr3:!s}ghi' Is equivalent to: 'abc' + expr1.format(spec1) + repr(expr2).format(spec2) + 'def' + str(expr3).format('') + 'ghi' But that's not correct. The right way to call format is: type(expr1).format(expr1, spec1) That is, the lookup of format is done on the type, not the instance. Instead of calling format, I've changed the code generator to call format(expr1, spec1). As an optimization, I might add special opcodes to deal with this and string concatenation, but that's for another day (if ever). I've posted a new version of the code in issue 24965. I'll update the PEP itself sometime this weekend.
Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (python.org/~guido) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20150919/26d025b2/attachment.html>
- Previous message (by thread): [Python-Dev] PEP 498 (interpolated f-string) tweak
- Next message (by thread): [Python-Dev] PEP 498 (interpolated f-string) tweak
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]