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】:
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