为什么print在python的print函数3中变成了函数

python2.x和3.x中的输出语句有着明显不同
2.x中的print不是个函数,输出格式如下
<span style="color: # Python 2.7.12+ (default, Aug
<span style="color: # [GCC 6.1.1 ] on linux2
<span style="color: # Type "help", "copyright", "credits" or "license" for more information.
<span style="color: # &&& print "There is only %d %s in the sky."%(1,'sun')
<span style="color: # There is only 1 sun in the sky.
3.x中的print成了函数,输出格式如下
<span style="color: # Python 3.5.2+ (default, Aug
<span style="color: # [GCC 6.1.1 ] on linux
<span style="color: # Type "help", "copyright", "credits" or "license" for more information.
<span style="color: # &&& print("There is only %d %s in the sky."%(1,'sun'))
<span style="color: # There is only 1 sun in the sky.
为什么要做出这样的变化,主要原因有以下几点:
1.print不是函数,不能使用help(),对使用者不方便。
python2中help(print)会报错。
<span style="color: # &&& help(print)
<span style="color: #
File "&stdin&", line 1
<span style="color: #
help(print)
<span style="color: #
<span style="color: # SyntaxError: invalid syntax
python3中,可以使用help(print),清楚的看到print的参数。
1 Help on built-in function print in module builtins:
3 print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
a file-like object (stream); defaults to the current sys.stdout.
string inserted between values, default a space.
<span style="color: #
string appended after the last value, default a newline.
<span style="color: #
flush: whether to forcibly flush the stream.
<span style="color: # (END)
2.从上面的help(print)中我们也可以看到在print()中的两个重要参数,sep和end。这两个参数使print()相比print多了两个新功能,自定义间隔符(默认空格)和结束符(默认回车)。
<span style="color: # &&& print("<span style="color: #3","<span style="color: #6","<span style="color: #9")
<span style="color: # 123 456 789
<span style="color: # &&& print("<span style="color: #3","<span style="color: #6","<span style="color: #9",sep='-')
<span style="color: # 123-456-789
<span style="color: # &&& x=1024
<span style="color: # &&& print(t)
<span style="color: # 256
<span style="color: # &&& print(t,end=" end")
<span style="color: # 256 end&&&
<span style="color: # &&& print(t,end=" end\n")
<span style="color: # 256 end
3.print()重定向输出文件更加方便。
2.x需要print&&重定向输出,感觉代码很混乱。
<span style="color: # &&& out=open("test.txt","w")
<span style="color: # &&& print&&out,"<span style="color: #3"
3.x中输出文件成了一个参数,使用更方便。
<span style="color: # &&& out=open("test.txt","w")
<span style="color: # &&& print("<span style="color: #3",file=out)
4.python2.x中print语句的格式化输出源自于C语言的格式化输出,这种语法对于C这种静态语言比较适用,但是对于拥有很多先进数据结构的python来说就有点力不从心了。python的元组,列表,字典,集合等不适合用这种结构表示,这些数据结构大多元素用下标表示,在这种结构中写出来很混乱。python3.x的print()函数提供了有点类似C#(不知道这么说对不对)中的格式化输出函数format()。另外print()也兼容原来的格式化输出方式。
<span style="color: # &&& print("%s is %s."%('Aoko','good'))
<span style="color: # Aoko is good.
format()让输出格式更清晰。
<span style="color: # &&& print("{0} is {1}.".format('Aoko','good'))
<span style="color: # Aoko is good.
format()支持数组下标,使python中的一些数据结构输出更加方便。
<span style="color: # &&& name=["Kaito",5]
<span style="color: # &&& print("{0[0]} has {0[1]} dollars.".format(name))
<span style="color: # Kaito has 5 dollars.
format()下的格式限定符,和原来的差不多。
<span style="color: # &&& x=5.6
<span style="color: # &&& print("{0:4f}".format(x))
<span style="color: # 5.600000
由此看来,print()相比print还是有很大进步的。说句题外话,我希望更多的python用户多花点时间实现代码对新版本的兼容,而不是花时间用在争论“python2和python3谁更好”的口水战上。python作为一种免费语言给我们带来了很多方便,我们不应该吝惜自己那么一点时间。花一点时间让python发展下去,变得更强。
阅读(...) 评论()他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)为什么 print 在 Python 3 中变成了函数?
10:34:45 +08:00 · 1067 次点击
在 Python 2 中, print 是一个语句( statement );而在 Python 3 中变成了函数( function )。很多 Python 用户都会问,为什么 Python 3 将 print 变成了函数呢?
一起来看看 Python 核心开发者 Brett Cannon 对此的解释。
& & 11:39:34 +08:00
不就是可变长度参数作为语句来处理太麻烦么
& & 11:01:44 +08:00
你把这个问题转成英文,拿 google 搜索,可以找到 python 官方社区的建议和讨论。所有 python 的新特性都是这样由提议,讨论过的,最后由大君裁决。那里的说明比你想象的答案要严谨和丰富。
& · & 1642 人在线 & 最高记录 3541 & · &
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.0 · 56ms · UTC 04:55 · PVG 12:55 · LAX 20:55 · JFK 23:55? Do have faith in what you're doing.

我要回帖

更多关于 python print函数用法 的文章

 

随机推荐