程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Why does Python 3 change print to a function?

編輯:Python

Abstract
The title says it all —— Ben PEP Proposed new built-in functions print() To replace print sentence , It is also recommended to use a special signature for this new function (signature ).

Explain the principle
print sentence It has long been on the list of unreliable language features , for example Guido Of “Python Regret ”(Python Regrets) speech 【1】, And plan to Python 3000 Version removal . therefore , Ben PEP The purpose of is not new , Although it may be in Python There is a lot of controversy among developers .

The following for print() The argument for the function is that it is extracted from Guido My Python-3000 news 【2】:

  • print Is the only application level feature , And have exclusive statements . stay Python In the world of , When some tasks cannot be completed without the help of the compiler , grammar (syntax) Usually used as a last resort . In this abnormal situation ,print Inappropriate .
  • When developing applications , People often need to replace... With something more complicated print Output , For example, call logging, Or call another I/O library . as for print() function , This is a straightforward character substitution , Now it mixes and matches all those parentheses , It is also possible to convert >>stream The grammar of style .
  • by print Setting up special grammars will only provide a greater barrier to evolution , For example, there is a conjecture , A new printf() The function will appear soon , Follow print() Function coexistence .
  • When a different separator is required ( It's not a space , Or no delimiter ) when , There is no easy way to print Statement to another call . similarly , When using other delimiters instead of spaces , There is no easy way to print objects .
  • If print() Is a function , You can easily replace it in a module ( Only def print(*args):…), It can even be replaced throughout the program ( For example, put a different method into __builtin__.print). actually , To do that , You can also write a tape write() Class of method , Then directed to sys.stdout , That's a good idea , But it is undoubtedly a great conceptual leap , And follow print comparison , It works at different levels .

Design specifications
print() The way of writing is taken from various emails , Recently released in python-3000 In the list is 【3】:

def print(*args, sep=' ', end='\n', file=None)

Call like :

print(a, b, c, file=sys.stderr)
Equivalent to the current :

print >>sys.stderr, a, b, c
Optional sep And end Parameters specify the contents between and after each print parameter accordingly .

softspace function ( The current semi secret attribute on the file , Used to tell print Do you want to insert a space in the first bar ) Will be deleted . therefore , The following expressions in the current version cannot be directly converted :

print "a",
print

It won't be in “a” Print a space between and newline .

( Translation notes : stay 3.3 edition ,print() The function has been changed again , Added default parameters flush=False)

Backward compatibility
Ben PEP The changes proposed in will lead to today's print Statement invalidation . Only those expressions that exactly enclose all parameters in parentheses can be used in Python 3 Version in effect , As for others , Only bracketed values can be printed as is . for example , stay 2.x in :

>>> print ("Hello")
Hello
>>> print ("Hello", "world")
('Hello', 'world')

And in the 3.0 in :

>>> print ("Hello")
Hello
>>> print ("Hello", "world")
Hello world

Fortunately, , because print yes Python 2 One of the statements in , So it can be detected by automated tools , And reliably and accurately replace , So there should be no major migration issues ( If someone writes this tool ).

Realization
Will change in Python 3000 Implement... In the branch ( Revision from 53685 To 53704). Most are in viku code (legacy code) The conversion has been made , But grab every one in the distribution print sentence , It still needs continuous efforts .

The above is all the content shared this time , Want to know more python Welcome to official account :Python Programming learning circle , send out “J” Free access to , Daily dry goods sharing


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved