[Python-ideas] Custom string prefixes

Göktuğ Kayaalp goktug.kayaalp at gmail.com
Mon May 27 15:58:37 CEST 2013


Thanks for your input, Steven, it's very blissful to be able to discuss
with people here.

> Do you have any idea how you would implement this change? Do you at least have
> an idea for the API?

I neither have an idea on how to implement this nor a good API design
idea. It has not been more than a couple weeks since when this came to
my mind and I've spent some time on trying to figure out how one would
benefit this. But, I can say that this idea is not original, as Scala
does have a concept that is probably similar (I believe Scala let's
one define a 'string interpolator', but I don't know very much detail).

> ... 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.
> ...
> 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
> ...
> 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.

I'll say that these are not valid statements (in my perspective) for the
following reasons:

    - We have decorators, which are only syntactic sugar for calling
      higher order functions, and, only higher order functions that take
      at least one argument and return a callable.

    - It does not only save half a second, but, it enables eliminate
      redundancy in code where string operations are very frequent.
      Some examples are scripts build tools[*], command line programs
      that process stdin and write results to stdout, logging utilities,
      etc.

[*] I use an utility that I've wrote, and I can say such a feature would
    reduce the density of the scripts I wrote. There is also Scons (and
    possibly others), but I don't know much about it.

Steven D'Aprano <steve at pearwood.info> writes:

> 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.

-- 
Göktuğ Kayaalp <goktug.kayaalp at gmail.com>


More information about the Python-ideas mailing list