Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 295 – Interpretation of multiline string constants

Author:
Stepan Koltsov <yozh at mx1.ru>
Status:
Rejected
Type:
Standards Track
Created:
22-Jul-2002
Python-Version:
3.0
Post-History:


Table of Contents

Abstract

This PEP describes an interpretation of multiline string constantsfor Python. It suggests stripping spaces after newlines andstripping a newline if it is first character after an openingquotation.

Rationale

This PEP proposes an interpretation of multiline string constantsin Python. Currently, the value of string constant is all thetext between quotations, maybe with escape sequences substituted,e.g.:

deff():"""    la-la-la    limona, banana    """defg():return"This is\    string"printrepr(f.__doc__)printrepr(g())

prints:

'\n\tla-la-la\n\tlimona, banana\n\t''This is\tstring'

This PEP suggest two things:

  • ignore the first character after opening quotation, if it isnewline
  • ignore in string constants all spaces and tabs up tofirst non-whitespace character, but no more than currentindentation.

After applying this, previous program will print:

'la-la-la\nlimona, banana\n''This is string'

To get this result, previous programs could be rewritten forcurrent Python as (note, this gives the same result with newstrings meaning):

deff():"""\la-la-lalimona, banana"""defg():"This is\string"

Or stripping can be done with library routines at runtime (aspydoc does), but this decreases program readability.

Implementation

I’ll say nothing about CPython, Jython or Python.NET.

In original Python, there is no info about the current indentation(in spaces) at compile time, so space and tab stripping should bedone at parse time. Currently no flags can be passed to theparser in program text (likefrom__future__importxxx). Isuggest enabling or disabling of this feature at Python compiletime depending of CPP flagPy_PARSE_MULTILINE_STRINGS.

Alternatives

New interpretation of string constants can be implemented with flags‘i’ and ‘o’ to string constants, like:

i"""SELECT * FROM carWHERE model = 'i525'"""isinnewstyle,o"""SELECT * FROM employeeWHERE birth < 1982"""isinoldstyle,and"""SELECT employee.name, car.name, car.price FROM employee, carWHERE employee.salary * 36 > car.price"""isinnewstyleafterPython-x.y.zandinoldstyleotherwise.

Also this feature can be disabled if string is raw, i.e. if flag ‘r’specified.

Copyright

This document has been placed in the Public Domain.


Source:https://github.com/python/peps/blob/main/peps/pep-0295.rst

Last modified:2025-02-01 08:55:40 GMT


[8]ページ先頭

©2009-2025 Movatter.jp