python2和python3的print语句语法有什么不同_与python 2 print语句相比,python 3.x中新的print函数有什么优势?...

我听过很多次,打印是一个函数(3.x)比它是一个语句(2.x)要好。但是为什么呢?

我非常喜欢它是一个语句,主要是因为它允许我少键入两个字符(即括号)。

我有兴趣看到一些情况下,打印语句只是不削减它,一个函数是优越的。

@伦纳特雷格布罗:这不仅仅是两个角色:整个流程感觉不同。不将其作为语句会产生成本-尤其是对于那些用于语句的。但它可以减轻一些角落案件的痛苦。

是的,现在您必须跟踪额外的圆括号,如果您还用元组嵌入内嵌字符串替换,那么连接多个字符串会稍微麻烦一些。因为这个,我在PY3中得到了更多的打字错误/无效语法错误!它导致我现在使用一个变量来生成打印的文本。

Rationale

The print statement has long appeared on lists of dubious language features that are to be removed in Python 3000, such as Guido's"Python Regrets" presentation [1]. As such, the objective of this PEP is not new, though it might become much disputed among Python developers.

The following arguments for a print() function are distilled from a python-3000 message by Guido himself [2]:

print is the only application-level functionality that has a statement dedicated to it. Within Python's world, syntax is generally used as a last resort, when something can't be done without help from the compiler. Print doesn't qualify for such an exception.

At some point in application development one quite often feels the need to replace print output by something more sophisticated, like logging calls or calls into some other I/O library. With a print() function, this is a straightforward string replacement, today it is a mess adding all those parentheses and possibly converting >>stream style syntax.

Having special syntax for print puts up a much larger barrier for evolution, e.g. a hypothetical new printf() function is not too far fetched when it will coexist with a print() function.

There's no easy way to convert print statements into another call if one needs a different separator, not spaces, or none at all. Also, there's no easy way at all to conveniently print objects with some other separator than a space.

If print() is a function, it would be much easier to replace it within one module (just def print(*args):...) or even throughout a program (e.g. by putting a different function in __builtin__.print). As it is, one can do this by writing a class with a write() method and assigning that to sys.stdout – that's not bad, but definitely a much larger conceptual leap, and it works at a different level than print.

— PEP 3105 – Make print a function

很好,但没有说明为什么表达式(函数调用)比语句更容易处理语法结构,尤其是简单的lambda。

从乔森的回答到斯文的回答,再加上:

您可以在不能使用print的地方使用print(),例如:

[print(x) for x in range(10)]

我故意不提这一点,因为我想不出我建议使用print()作为一种表达,而不是一种陈述。您的示例创建了一个不需要的列表,该列表的值是None的10倍,当然最好使用for x in range(10): print(x)。

@ Sven:当然可以。我当然不想让它成为公认的答案。:)

我刚刚尝试在lambda中使用print,其中一个类允许我传递一个函数来记录值。我只想把它们打印出来。我必须在其他地方定义一个函数,因为您不能编写lambda x: print x:(。

它还使构建语言python的聪明人更容易添加诸如不同分隔符之类的功能。

print作为函数的一个优点是一致性。它没有理由成为一个声明。比较这两行

2.x: print >> my_file, x

3.x: print(x, file=my_file)

新版本看起来更像python,不是吗?

函数版本的另一个优点是灵活性。例如,如果您想捕获所有用于调试目的的print调用,现在只需重新定义print:

def print(*args, **kwargs):

# whatever

__builtins__.print(*args, **kwargs)

新版本看起来更像python,不是吗?":它们都是有效的python,不是吗?我相信你的意思是"Python",但即使是"Python"也很含糊。我不同意你的回答,我只是觉得你的问题对print函数来说是一个不足的论点。(我认为,OP的答案授权给了心理学家和管理者:)

_Ω__933;:我说过这是关于一致性的。这不是一个困难的事实,所以我必须最终把判断留给读者。如果你愿意的话,我可以说"在我看来,新版本更像python"。:)

我考虑过这个问题,不知道Python3版本的优点。但当我需要打印pandas.DataFrame列(没有Index([...]))时,我发现

print *df.columns

引发异常,而

print(*df.columns)

工作很好!如果您想在多个调用打印中使用相同的(可配置的)打印选项,可以将它们保存到字典中并作为**print_options传递。

所以至少*args和**kw_args技巧是print发挥作用的一个很好的理由!

上票,因为我可以举一个例子

您可以将内置的print替换为自定义的:

import os

import sys

def print(s):

sys.stderr.write('Will now print ' + str(s) + '.' + os.linesep)

sys.stdout.write(str(s) + os.linesep)

print(['A', 'list'])

# Output:

# stderr: Will now print ['A', 'list'].

# stdout: ['A', 'list']

您可以在lambda或函数调用等内部使用print:

example_timeout_function(call=lambda: print('Hello world'), timeout=5)

do_things(print_function=print)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值