预PEP:打印时没有干预空间 [英] pre-PEP: Print Without Intervening Space

查看:60
本文介绍了预PEP:打印时没有干预空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我最近写的关于印刷品的前PEP。

请让我知道社区对它的看法是什么。


干杯,

Marcin

PEP:XXX

标题:没有干预空间打印

版本:$修订版: 0.0 $

作者:Marcin Ciura< marcin.ciura at polsl.pl>

状态:草稿

类型:Standards Track

创建时间:2005年3月11日

历史后:2005年3月11日

摘要


本PEP建议扩展print语句

的语法,以便通过使用double而不是单个逗号来选择性地禁用其空格插入机制



理由


print语句可以在一行中写几个表达式,

但是现在总是用空格分隔它们。虽然这种

行为通常是可取的,但并非罕见的情况,其中

程序员必须使用变通方法来实现非间隔的b
显示。这被认为是Python Gotchas之一

[1]。即使是最简单的解决方法也会导致不必要的b $ b b复杂代码(为了简单起见,我们假设

fn()返回字符串):

result =''''

for x in seq:

result + = fn(x)

打印结果


更不用说它也有一个糟糕的算法复杂性。

没有一个更有效的解决方案特别是

直接,或者:


结果= []

for x in seq:

result.append(fn(x))

print''''。join(结果)


print''''。join([fn(x)for x in seq])


print''''。join(fn(x)for x in seq)


此外,所有这些都需要创建一个或两个临时的

对象来保存整个结果。如果程序员使用

中的一个没有问题,那只是因为他们的思维被打印限制了




使用write()也不是特别吸引人,特别是如果

打印语句在代码的其他地方使用:


import sys

for x in seq:

sys.stdout.write(fn(x))

print#或sys.stdout.write(''\ n '')


打印声明的建议扩展是使用两个

逗号表示在
$之后不应写入任何空格b $ b表达式:


for x in seq:

print fn(x),,

print


引用The Zen of Python [2]:美丽胜过丑陋。

简单比复杂更好。可读性计数。


该提案也适用于

中间的表达式print语句。因此它提供了字符串

连接和字符串插值的替代方法,可以使用''%''基于

说明符,也可以使用''$''基于由PEP 292 [3]引入的,

不需要创建临时字符串对象:


print''电话号码是('',扩展名,,''''',数字,,''。''


请注意,我并未声称上述版本已经更多

可读比


打印''电话号码是(%s)%s。''%(分机,号码)

规格


建议允许用单逗号或双逗号分隔要打印的表达式

,并允许单个或双重逗号

结束时印刷声明。这两个逗号应连续是
,即它们之间不应有空格。

非连续逗号或两个以上逗号的任何序列

构成语法错误。在print vvron中。

语句的形式,文件对象的名称应与

分开,下一个表达式只能用一个逗号,就像现在一样。


形式上,扩展打印语句的建议语法是


print_stmt:" print"
([表达式(&) ;," |" ,,")expression)* ["," |" ,,"]]

|">>" expression [( ","

expression(("," |" ,,")expression)* ["," |","]]

实现建议的语法可能需要引入一个新的
类型的令牌:双逗号,或解析器中的hack来识别

two print语句中的连续逗号。


两个新的字节代码,与PRINT_ITEM和PRINT_ITEM_TO并行,是实现提案语义所需的


讨论


优点:


- T他提出语义允许在执行print语句期间避免使用临时字符串对象

,并且通常会使

更具可读性和显式源代码。


- 建议的语法很容易为初学者学习。


- 它没有打破现有的Python代码。


- 错误建议的语法不太可能发生,

,除非有人打字或键盘有问题,

在这种情况下,任何编程都很困难,无论如何。


缺点:


- 打印周围的包装函数将无法模仿其

语法。然而,即使是现在也是不可能的,因为尾随的是逗号。


- 在PEP 259 [4]中,BDFL声称他想要避免

再修补打印。


- PEP 3000 [5]和Python Regrets [6]声明打印

语句将被Python 3000中的函数替换,

因此扩展它可能是一条死路径。

参考资料


[1] Python Gotchas,Steve Ferg:
http://www.ferg.org/projects/python_gotchas.html


[2] Python的禅,蒂姆彼得斯
http://www.python.org/doc/Humor .html


[3] PEP 292,简单字符串替换,Barry A.华沙:
http://www.python.org/peps/pep-0292.html


[4] PEP 259,换行后省略印刷换行符,

Guido van Rossum:
http://www.python.org/peps/pep-0259.html


[5] PEP 3000,Python 3.0计划,A.M。 Kuchling,Brett Cannon:
http://www.python .org / peps / pep-3000.html


[6] Python遗憾,Guido van Rossum:
http://www.python.org/doc/essays/ppt...honRegrets.pdf

版权


本文档已被置于公共领域。



...

本地变量:

模式:缩进文本

缩进标签模式:无>

句末 - 双倍空格:t

填充栏:70

结束:

Here is a pre-PEP about print that I wrote recently.
Please let me know what is the community''s opinion on it.

Cheers,
Marcin
PEP: XXX
Title: Print Without Intervening Space
Version: $Revision: 0.0 $
Author: Marcin Ciura <marcin.ciura at polsl.pl>
Status: Draft
Type: Standards Track
Created: 11-Mar-2005
Post-History: 11-Mar-2005
Abstract

This PEP proposes to extend the syntax of the print statement
so that its space-insertion mechanism can be selectively
disabled by using double instead of single commas.
Rationale

The print statement can write several expressions in one line,
but presently always separates them with spaces. While this
behaviour is often desirable, not uncommon are situations, where
programmers have to use workarounds to achieve a non-spaced
display. This has been recognized as one of "Python Gotchas"
[1]. Even the simplest workaround results in an unnecessarily
complicated code (for the sake of simplicity let us assume that
fn() returns strings):

result = ''''
for x in seq:
result += fn(x)
print result

Not to mention it also has a terrible algorithmic complexity.
None of the more efficient solutions is particularly
straightforward, either:

result = []
for x in seq:
result.append(fn(x))
print ''''.join(result)

print ''''.join([fn(x) for x in seq])

print ''''.join(fn(x) for x in seq)

Moreover, all of them require creating one or two temporary
objects to hold the entire result. If the programmers use one of
them without qualms, it is only because their mind is warped by
the limitation of print.

Using write() is not especially appealing either, especially if
the print statements are used elsewhere in the code:

import sys
for x in seq:
sys.stdout.write(fn(x))
print # or sys.stdout.write(''\n'')

The proposed extension to the print statement is to use two
commas to signal that no space should be written after an
expression:

for x in seq:
print fn(x),,
print

To quote "The Zen of Python" [2]: "Beautiful is better than ugly.
Simple is better than complex. Readability counts."

The proposal applies also to the expressions in the middle of
the print statement. Thus it provides an alternative to string
concatenation and string interpolation, either with the ''%''-based
specifiers, or with the ''$''-based ones introduced by PEP 292 [3],
not requiring creating a temporary string object:

print ''The phone number is ('',,extension,,'')'', number,,''.''

Note that I do not claim that the above version is any more
readable than

print ''The phone number is (%s) %s.'' % (extension, number)
Specification

It is proposed to allow separating the expressions to be printed
by single or double commas, and to allow single or double commas
at the end of the print statement. The two commas shall be
consecutive, i.e. there shall be no whitespace between them.
Non-consecutive commas or any sequence of more than two commas
constitute a syntax error. In the "print chevron" form of the
statement, the name of the file object shall be separated from
the next expression only by a single comma, as it is now.

Formally, the proposed syntax of the extended print statement is

print_stmt: "print"
( [expression (("," | ",,") expression)* ["," | ",,"]]
| ">>" expression [(","
expression (("," | ",,") expression)* ["," | ",,"]]

Implementing the proposed syntax may require introducing a new
type of token: double comma, or a hack in the parser to recognize
two consecutive commas in the context of the print statement.

Two new byte codes, parallel to PRINT_ITEM and PRINT_ITEM_TO, are
needed to implement the semantics of the proposal.
Discussion

Pros:

- The proposed semantics allows avoiding temporary string objects
during the execution of the print statement and often makes for
more readable and explicit source code.

- The proposed syntax is easy to learn for the beginners.

- It breaks no existing Python code.

- Mistakes are unlikely to happen with the proposed syntax,
unless someone has problems with his typing or his keyboard,
in which case any programming is difficult, anyway.

Cons:

- Wrapper functions around print will be unable to mimic its
syntax. It is, however, impossible even now, due to trailing
commas.

- In PEP 259 [4], the BDFL has pronounced that he wants to avoid
any more tinkering with "print".

- PEP 3000 [5] and "Python Regrets" [6] state that the print
statement is to be replaced with a function in Python 3000,
so extending it may be a dead path.
References

[1] Python Gotchas, Steve Ferg:
http://www.ferg.org/projects/python_gotchas.html

[2] The Zen of Python, Tim Peters
http://www.python.org/doc/Humor.html

[3] PEP 292, Simpler String Substitutions, Barry A. Warsaw:
http://www.python.org/peps/pep-0292.html

[4] PEP 259, Omit printing newline after newline,
Guido van Rossum:
http://www.python.org/peps/pep-0259.html

[5] PEP 3000, Python 3.0 Plans, A.M. Kuchling, Brett Cannon:
http://www.python.org/peps/pep-3000.html

[6] Python Regrets, Guido van Rossum:
http://www.python.org/doc/essays/ppt...honRegrets.pdf
Copyright

This document has been placed in the public domain.


...
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
End:

推荐答案

修订版:0.0




作者:Marcin Ciura< marcin.ciura at polsl.pl>

状态:草稿

类型:标准轨道

创建时间:2005年3月11日

后历史:2005年3月11日

摘要

此PEP建议扩展print语句的语法

,以便通过使用double而不是单个逗号来选择性地禁用其空格插入机制



基本原理


print语句可以在一行中写几个表达式,

但是现在总是用空格分隔它们。虽然这种

行为通常是可取的,但并非罕见的情况,其中

程序员必须使用变通方法来实现非间隔的b
显示。这被认为是Python Gotchas之一

[1]。即使是最简单的解决方法也会导致不必要的b $ b b复杂代码(为了简单起见,我们假设

fn()返回字符串):

result =''''

for x in seq:

result + = fn(x)

打印结果


更不用说它也有一个糟糕的算法复杂性。

没有一个更有效的解决方案特别是

直接,或者:


结果= []

for x in seq:

result.append(fn(x))

print''''。join(结果)


print''''。join([fn(x)for x in seq])


print''''。join(fn(x)for x in seq)


此外,所有这些都需要创建一个或两个临时的

对象来保存整个结果。如果程序员使用

中的一个没有问题,那只是因为他们的思维被打印限制了




使用write()也不是特别吸引人,特别是如果

打印语句在代码的其他地方使用:


import sys

for x in seq:

sys.stdout.write(fn(x))

print#或sys.stdout.write(''\ n '')


打印声明的建议扩展是使用两个

逗号表示在
$之后不应写入任何空格b $ b表达式:


for x in seq:

print fn(x),,

print


引用The Zen of Python [2]:美丽胜过丑陋。

简单比复杂更好。可读性计数。


该提案也适用于

中间的表达式print语句。因此它提供了字符串

连接和字符串插值的替代方法,可以使用''%''基于

说明符,也可以使用''

Author: Marcin Ciura <marcin.ciura at polsl.pl>
Status: Draft
Type: Standards Track
Created: 11-Mar-2005
Post-History: 11-Mar-2005
Abstract

This PEP proposes to extend the syntax of the print statement
so that its space-insertion mechanism can be selectively
disabled by using double instead of single commas.
Rationale

The print statement can write several expressions in one line,
but presently always separates them with spaces. While this
behaviour is often desirable, not uncommon are situations, where
programmers have to use workarounds to achieve a non-spaced
display. This has been recognized as one of "Python Gotchas"
[1]. Even the simplest workaround results in an unnecessarily
complicated code (for the sake of simplicity let us assume that
fn() returns strings):

result = ''''
for x in seq:
result += fn(x)
print result

Not to mention it also has a terrible algorithmic complexity.
None of the more efficient solutions is particularly
straightforward, either:

result = []
for x in seq:
result.append(fn(x))
print ''''.join(result)

print ''''.join([fn(x) for x in seq])

print ''''.join(fn(x) for x in seq)

Moreover, all of them require creating one or two temporary
objects to hold the entire result. If the programmers use one of
them without qualms, it is only because their mind is warped by
the limitation of print.

Using write() is not especially appealing either, especially if
the print statements are used elsewhere in the code:

import sys
for x in seq:
sys.stdout.write(fn(x))
print # or sys.stdout.write(''\n'')

The proposed extension to the print statement is to use two
commas to signal that no space should be written after an
expression:

for x in seq:
print fn(x),,
print

To quote "The Zen of Python" [2]: "Beautiful is better than ugly.
Simple is better than complex. Readability counts."

The proposal applies also to the expressions in the middle of
the print statement. Thus it provides an alternative to string
concatenation and string interpolation, either with the ''%''-based
specifiers, or with the ''


'' - 基于PEP 292引入的[3],

不需要创建临时字符串对象:


print''电话号码是('',, extension ,,'')'',数字,''。''


请注意,我并未声称上述版本是任何更多

可读比


打印''电话号码是(%s)%s。''%(分机号码)

规格


建议允许用单逗号或双逗号分隔要打印的表达式

,并允许单个或双重逗号

在print语句的末尾。这两个逗号应连续是
,即它们之间不应有空格。

非连续逗号或两个以上逗号的任何序列

构成语法错误。在print vvron中。

语句的形式,文件对象的名称应与

分开,下一个表达式只能用一个逗号,就像现在一样。


形式上,扩展打印语句的建议语法是


print_stmt:" print"
([表达式(&) ;," |" ,,")expression)* ["," |" ,,"]]

|">>" expression [( ","

expression(("," |" ,,")expression)* ["," |","]]

实现建议的语法可能需要引入一个新的
类型的令牌:双逗号,或解析器中的hack来识别

two print语句中的连续逗号。


两个新的字节代码,与PRINT_ITEM和PRINT_ITEM_TO并行,是实现提案语义所需的


讨论


优点:


- T他提出语义允许在执行print语句期间避免使用临时字符串对象

,并且通常会使

更具可读性和显式源代码。


- 建议的语法很容易为初学者学习。


- 它没有打破现有的Python代码。


- 错误建议的语法不太可能发生,

,除非有人打字或键盘有问题,

在这种情况下,任何编程都很困难,无论如何。


缺点:


- 打印周围的包装函数将无法模仿其

语法。然而,即使是现在也是不可能的,因为尾随的是逗号。


- 在PEP 259 [4]中,BDFL声称他想要避免

再修补打印。


- PEP 3000 [5]和Python Regrets [6]声明打印

语句将被Python 3000中的函数替换,

因此扩展它可能是一条死路径。

参考资料


[1] Python Gotchas,Steve Ferg:
http://www.ferg.org/projects/python_gotchas.html


[2] Python的禅,蒂姆彼得斯
http://www.python.org/doc/Humor .html


[3] PEP 292,简单字符串替换,Barry A.华沙:
http://www.python.org/peps/pep-0292.html


[4] PEP 259,换行后省略印刷换行符,

Guido van Rossum:
http://www.python.org/peps/pep-0259.html


[5] PEP 3000,Python 3.0计划,A.M。 Kuchling,Brett Cannon:
http://www.python .org / peps / pep-3000.html


[6] Python遗憾,Guido van Rossum:
http://www.python.org/doc/essays/ppt...honRegrets.pdf

版权


本文档已被置于公共领域。



...

本地变量:

模式:缩进文本

缩进标签模式:无>

句末 - 双倍空格:t

填充栏:70

结束:
''-based ones introduced by PEP 292 [3],
not requiring creating a temporary string object:

print ''The phone number is ('',,extension,,'')'', number,,''.''

Note that I do not claim that the above version is any more
readable than

print ''The phone number is (%s) %s.'' % (extension, number)
Specification

It is proposed to allow separating the expressions to be printed
by single or double commas, and to allow single or double commas
at the end of the print statement. The two commas shall be
consecutive, i.e. there shall be no whitespace between them.
Non-consecutive commas or any sequence of more than two commas
constitute a syntax error. In the "print chevron" form of the
statement, the name of the file object shall be separated from
the next expression only by a single comma, as it is now.

Formally, the proposed syntax of the extended print statement is

print_stmt: "print"
( [expression (("," | ",,") expression)* ["," | ",,"]]
| ">>" expression [(","
expression (("," | ",,") expression)* ["," | ",,"]]

Implementing the proposed syntax may require introducing a new
type of token: double comma, or a hack in the parser to recognize
two consecutive commas in the context of the print statement.

Two new byte codes, parallel to PRINT_ITEM and PRINT_ITEM_TO, are
needed to implement the semantics of the proposal.
Discussion

Pros:

- The proposed semantics allows avoiding temporary string objects
during the execution of the print statement and often makes for
more readable and explicit source code.

- The proposed syntax is easy to learn for the beginners.

- It breaks no existing Python code.

- Mistakes are unlikely to happen with the proposed syntax,
unless someone has problems with his typing or his keyboard,
in which case any programming is difficult, anyway.

Cons:

- Wrapper functions around print will be unable to mimic its
syntax. It is, however, impossible even now, due to trailing
commas.

- In PEP 259 [4], the BDFL has pronounced that he wants to avoid
any more tinkering with "print".

- PEP 3000 [5] and "Python Regrets" [6] state that the print
statement is to be replaced with a function in Python 3000,
so extending it may be a dead path.
References

[1] Python Gotchas, Steve Ferg:
http://www.ferg.org/projects/python_gotchas.html

[2] The Zen of Python, Tim Peters
http://www.python.org/doc/Humor.html

[3] PEP 292, Simpler String Substitutions, Barry A. Warsaw:
http://www.python.org/peps/pep-0292.html

[4] PEP 259, Omit printing newline after newline,
Guido van Rossum:
http://www.python.org/peps/pep-0259.html

[5] PEP 3000, Python 3.0 Plans, A.M. Kuchling, Brett Cannon:
http://www.python.org/peps/pep-3000.html

[6] Python Regrets, Guido van Rossum:
http://www.python.org/doc/essays/ppt...honRegrets.pdf
Copyright

This document has been placed in the public domain.


...
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
End:


这篇关于预PEP:打印时没有干预空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆