Home » Python » [cold] knowledge of analog print function similar ...

[cold] knowledge of analog print function similar to 3.x in python2.7 or earlier

although it's not necessary to do so, in general, we can have a better way to implement:


, for example, uses from __future__ import print_function to implement:



from __future__ import print_function
Print (123456, end=', ' file = (open (' temp.txt' ' w'))
# which is roughly equivalent to you to write print within 2.x (' >>open, ' temp.txt' w' 123456),



, however, the following examples can help you better understand how print's built-in function works:


# coding: UTF-8 -*- -*-
Def print3 (*args, **kargs):
' ' '
Simulated print function
In 2.7 or earlier versions of python, simulate a print3 function that implements most of the 3.x versions
' ' '
Import sys
SEP = kargs.pop (' sep' ' ' pop, #) function dictionary dictionary to delete the specified key and returns a value, find the key to return to their set value if not.
End = kargs.pop (' end' ' n')
File = kargs.pop (' file', sys.stdout) # print stream oriented
If kargs: raise TypeError (' extra keywords:%s'%kargs) # throws an exception when the function is introduced into other strange parameters
Output = ' '
First = True
For Arg in args:
Output = (' ' if first else SEP) + str (ARG)
First = False
File.write (output + end) # file

Latest