[Python-Dev] PEP-498: Literal String Formatting

ISAAC J SCHWABACHER ischwabacher at wisc.edu
Thu Aug 13 23:57:08 CEST 2015


Well, I seem to have succeeded in crystallizing opinions on the topic, even if the consensus is, "Augh! Make it stop!" :)

The primary objective of that code sample was to make the structure of the code as close as possible to the structure of the interpolated string, since having descriptive text like "{entry.doc_cname}" inline instead of "%s" is precisely what str.format gains over str.__mod__.

But there are several different elements in that code, and I'm curious what people find most off-putting. Is it the triple quoted format strings? The nesting? The interpolation with `"""...""" if cond else ''`? Just plain interpolations as are already available with str.format, but without explicitly importing names into the format string's scope via **kwargs? Trying to emulate let? Would a different indentation scheme make things better, or is this a problem with the coding style I've advanced here, or with the feature itself?

Also, should this be allowed:

def make_frob(foo):
    def frob(bar):
        f"""Frob the bar using {foo}"""

?

ijs

P.S.: I've translated the original snippet into ruby here: https://gist.github.com/ischwabacher/405afb86e28282946cc5, since it's already legal syntax there.

Ironically, github's syntax highlighting either fails to parse the interpolation (in edit mode) or fails to treat the heredoc as a string literal (in display mode), but you can open it in your favorite editor to see whether the highlighting makes the code clearer.

________________________________________
From: Python-Dev <python-dev-bounces+ischwabacher=wisc.edu at python.org> on behalf of Ethan Furman <ethan at stoneleaf.us>
Sent: Wednesday, August 12, 2015 18:11
To: python-dev at python.org
Subject: Re: [Python-Dev] PEP-498: Literal String Formatting

On 08/10/2015 04:05 PM, ISAAC J SCHWABACHER wrote:

> I don't know about you, but I sure like this better than what you have:
>
> code.putlines(f"""
> static char {entry.doc_cname}[] = "{
>      split_string_literal(escape_bytestring(docstr))}";
>
> { # nested!
> f"""
> #if CYTHON_COMPILING_IN_CPYTHON
>      struct wrapperbase {entry.wrapperbase_cname};
> #endif
> """ if entry.is_special else ''}
>
> {(lambda temp, argn: # my kingdom for a let!
> f"""
> for ({temp}=0; {temp}<PyTuple_GET_SIZE({argn}); {temp}++) {{
>      PyObject *item = PyTuple_GET_ITEM({argn}, {temp});
> }}""")(..., Naming.args_cname)}
>
> {self.starstar_arg.entry.cname} =
>      ({Naming.kwds_cname}) ? PyDict_Copy({Naming.kwds_cname})
>                            : PyDict_New();
>
> if (unlikely(!{self.starstar_arg.entry.cname})) return {self.error_value()};
> """)
>
> What do others think of this PEP-498 sample?  (The PEP-501 version looks pretty similar, so I omit it.)

Agh!  My brain is hurting!  ;)

No, I don't care for it at all.

--
~Ethan~
_______________________________________________
Python-Dev mailing list
Python-Dev at python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/ischwabacher%40wisc.edu


More information about the Python-Dev mailing list