Chris Wilcox <pyt...@crwilcox.com> added the comment:
The attached code implements `__format__` on the `Collections` class. In case 1, the template passed to `__format__` is "{v.name}: {v.email}|". In case 2, a name error will occur while processing the f string and v will not be found as no object 'v' exists in locals or globals. In reviewing PEP 0498, https://www.python.org/dev/peps/pep-0498/, I think the difference is in what object is being formatted. In case 1 of the attached code, the collection is being formatted. In case 2 where f-strings are used, 'v' is being formatted. Because v doesn't exist in this context, it fails. I found this in the PEP and I think it is what is going on here. ``` Note that __format__() is not called directly on each value. The actual code uses the equivalent of type(value).__format__(value, format_spec), or format(value, format_spec). See the documentation of the builtin format() function for more details. ``` ---------- nosy: +crwilcox _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue39601> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com