Do you really understand hundred percent print function?

print function prototype

print this function should be a function of the most used, but the use of all functions are clear in your heart? Let's take a look at the structure of the print function.

def print(self, *args, sep=' ', end='\n', file=None): # known special case of 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:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
    """
    pass
  • *args:表示我们要打印的内容,是一个可变参数。
  • sep:分隔符,对打印的多个元素进行分隔,默认是空格
  • end:我们print打印的时候,会自动换行,就是因为这里的\n
  • file:输出位置,默认是输出到sys.stdout
  • flush:是否及时刷新到缓冲区。

Show

Let's modify the parameters inside, take a look at what different print results.

sep

# 我们看到打印的三个数字是以空格分开的,就是因为sep=' '
print(1, 2, 3)  # 1 2 3

# 我们修改一下,可以看到此时就是我们指定的分割符了
print(1, 2, 3, sep='>>>')  # 1>>>2>>>3

end

print(1, 2, 3)
print(4, 5, 6)
"""
1 2 3
4 5 6
"""

# 我们看到上面的两个print打印的时候,是位于不同的行
# 就是因为python中的print在打印之后会自动在结尾加上一个换行符,也就是end='\n',使得光标停在下一行
# 像在C语言中,由于printf不会自动加上换行符,所以会需要手动加上\n
# 我们这里修改一下,也把sep加上去。而我们的end已经被改为了@@@
print(1, 2, 3, sep='>>>', end='@@@')
print(4, 5, 6)
print(7, 8, 9)
"""
1>>>2>>>3@@@4 5 6
7 8 9
"""
# 此时我们看到4 5 6本来应该出现在第二行的,但是第一个print的end不再是\n了,而是@@@
# 因此不会再换行了,而是直接在结尾加上一个@@@,光标还是停在当前行,所以第二个print打印就连在一起了
# 但是第二个print我们没有指定end,那么默认还是\n,所以第三个print打印还是会在新的行
# 也说明了print之间不会互相影响

file

Here's what represents the output file location, the default is sys.stdout, which is the console.

import sys

print("hello1")
print("hello2", file=sys.stderr)
print("hello3")
"""
hello1
hello3
hello2
"""

But we see the string "hello2" obviously is printed in the second row, but the display has appeared in the final at the time of the console. This is because the buffer zone, which we saw the emergence of two output locations, one is the default sys.stdout, we are a single designated sys.stderr, different locations with different output buffers, so I can not guarantee which first display. Even if multiple execution, the result may not be the same.

import sys

print("hello1")
print("hello2", file=sys.stderr)
print("hello3")
# 此时顺序又变了
"""
hello2
hello1
hello3
"""

Similarly, we can also specify the file into a file handle, then obviously it will print the contents of a file is written inside. Note: Since we print is written, then the file handle must be writable.

f = open("1.txt", "a", encoding="utf-8")
print("hello1", file=f)
print("hello2", file=f)
print("hello3", file=f)

At this time, the terminal will not have any output, but we can look at the files, you will find what has been written into it. So print can also be simple to achieve functionality similar to the log.

flush

flush expressed refresh buffer, the buffer not have introduced me, if at every point data is refreshed once, it is inefficient. Thus data is temporarily written to the buffer which will, once all the other buffer is full brush into the terminal. flush default is False, if specified as True, then the buffer is full not indicate whether it is full, are forced to flush the buffer, the contents of which will be displayed on the screen.

print(123)
print(456, flush=True)
print(789)
"""
123
456
789
"""
# 这个不好展示,感受一下即可

Guess you like

Origin www.cnblogs.com/traditional/p/12236279.html