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

Stefan Behnel stefan_ml at behnel.de
Tue Aug 11 08:12:06 CEST 2015


ISAAC J SCHWABACHER schrieb am 11.08.2015 um 01:05:
> 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()};
> """)

Matter of taste, I guess. Looks awful to me. It's very difficult to
visually separate input and output in this code, so it requires a thorough
look to see what data is being used for the formatting.

Syntax highlighting and in-string expression completion should eventually
help, once IDEs support it. But then editing this code will require an
editor that has such support. And not everyone is going to be willing to
get one.

Stefan




More information about the Python-Dev mailing list