Here is a much more readable alternative which has the advantage that it is already supported by Python. I'm going to show a harder example where I want to reference foo.bar and allow both foo and bar to be refactored. Here's the original proposal:
f"The field {nameof(foo)}.{nameof(foo.bar)} can be refactored."
Here's my alternative:
f"""The field {"foo.bar"} can be refactored."""Since there's no real reason that you'd use {} around a literal string, we have our refactoring tool recognize that syntax as a variable reference. It has no net effect on the running code. If we don't want foo to be refactored, we write instead:
f"""The field foo.{"bar"} can be refactored."""
And if we just want the string to reference "bar" but want to make sure it's foo.bar not something_else.bar, we could write this:
f"""The field {"foo" and "bar"} can be refactored."""
--- Bruce
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/J2DZZLAOV72SS4XXJISX7KPZNJPYUZVE/ Code of Conduct: http://python.org/psf/codeofconduct/