[Python-ideas] Custom string prefixes

Steven D'Aprano steve at pearwood.info
Mon May 27 15:13:43 CEST 2013


On 27/05/13 20:41, Göktuğ Kayaalp wrote:

> I'm looking forward to your criticisms and advices. I've searched this
> online and asked in the chatroom (#python) and I'm nearly sure that I'm
> not asking for a feature that is already present. Being a beginner, I
> can say that I'm kind of nervous to post here, where really experienced
> people discuss the features of an internationally-adopted language.

Welcome, and I admire your bravery! So please don't take it personally when I say, your idea does not sound very good to me. In fact, it sounds terrible. You call this proposal "custom string prefixes", but what you describe is actually a second way to call a function, only not any function, but just functions that take a single string argument. So more like a function call that looks like a string.

Let me start with your example:


>          >>> from decimal import Decimal as D
>          >>> D ("1.6e-9") * D ("1.0e9")
>
>      but I'd enounce that the following is more concise and readable:
>
>          >>> D"1.6e-9" * D"1.0e9"
>
>      with removed parens.

Just to save a couple of parentheses, you add a lot of complication to the language, make it harder for people to learn, and for no real benefit except to save a few keystrokes. Consider:

String prefixes are currently part of Python's syntax, and can operate at compile-time. With your proposal, they become run-time operations, like any function call. So this is redundant: we already have a perfectly good way of calling functions. Not just redundant, but also very limited, because most functions take more than one argument, or non-string arguments.

Do you have any idea how you would implement this change? Do you at least have an idea for the API? What commands would the user give to define a new "string prefix"? How would the user query the "string prefixes" already defined? What happens when they combine multiple prefixes?

Python is famous for being "executable pseudo-code". To a very large degree, code written in Python should be readable by people who are not Python programmers. What do you think

s"ham"

will mean to the reader? I think that it is better to encourage people to write meaningful names:

make_sandwich("ham")

than trying to save every last keystroke possible. Code is written once, but read over and over again. That half a second you save by typing s"ham" will cost other people dozens of seconds, maybe minutes, each time them read your code.




-- 
Steven


More information about the Python-ideas mailing list