On Another Subject, we could also have a language change staying that those two lines are equivalent :
something"hello" something("hello") So that, any callable in the context can be used as a prefix ? On Tue, Aug 27, 2019, 01:11 <stpa...@gmail.com> wrote: > In Python strings are allowed to have a number of special prefixes: > > b'', r'', u'', f'' > + their combinations. > > The proposal is to allow arbitrary (or letter-only) user-defined prefixes > as well. > Essentially, a string prefix would serve as a decorator for a string, > allowing the > user to impose a special semantics of their choosing. > > There are quite a few situations where this can be used: > - Fraction literals: `frac'123/4567'` > - Decimals: `dec'5.34'` > - Date/time constants: `t'2019-08-26'` > - SQL expressions: `sql'SELECT * FROM tbl WHERE a=?'.bind(a=...)` > - Regular expressions: `rx'[a-zA-Z]+'` > - Version strings: `v'1.13.0a'` > - etc. > > This proposal has been already discussed before, in 2013: > > https://mail.python.org/archives/list/python-ideas@python.org/thread/M3OLUURUGORLUEGOJHFWEAQQXDMDYXLA/ > > The opinions were divided whether this is a useful addition. The opponents > mainly argued that as this only "saves a couple of keystrokes", there is no > need to overcomplicate the language. It seems to me that now, 6 years > later, > that argument can be dismissed by the fact that we had, in fact, added new > prefix "f" to the language. Note how the "format strings" would fall > squarely > within this framework if they were not added by now. > > In addition, I believe that "saving a few keystroked" is a worthy goal if > it adds > considerable clarity to the expression. Readability counts. Compare: > > v"1.13.0a" > v("1.13.0a") > > To me, the former expression is far easier to read. Parentheses, > especially as > they become deeply nested, are not easy on the eyes. But, even more > importantly, > the first expression much better conveys the *intent* of a version string. > It has > a feeling of an immutable object. In the second case the string is passed > to the > constructor, but the string has no meaning of its own. As such, the second > expression feels artificial. Consider this: if the feature already > existed, how *would* > you prefer to write your code? > > The prefixes would also help when writing functions that accept different > types > of their argument. For example: > > collection.select("abc") # find items with name 'abc' > collection.select(rx"[abc]+") # find items that match regular > expression > > I'm not discussing possible implementation of this feature just yet, we > can get to > that point later when there is a general understanding that this is worth > considering. > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@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/3Z2YTIGJLSYMKKIGRSFK2DTDIXXVDGEK/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@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/5VEZTGHHWIGL46LNYBKJB5BAPUBCLTCM/ Code of Conduct: http://python.org/psf/codeofconduct/