Python Built-in Functions (内置函数) print()

Built-in Functions (内置函数)
https://docs.python.org/3/library/functions.html
https://docs.python.org/zh-cn/3/library/functions.html

1. 2to3 - Automated Python 2 to 3 code translation (自动将 Python 2 代码转为 Python 3 代码)

https://docs.python.org/3/library/2to3.html

2to3 is a Python program that reads Python 2.x source code and applies a series of fixers to transform it into valid Python 3.x code. The standard library contains a rich set of fixers that will handle almost all code. 2to3 supporting library lib2to3 is, however, a flexible and generic library, so it is possible to write your own fixers for 2to3. lib2to3 could also be adapted to custom applications in which Python code needs to be edited automatically.
2to3 是一个 Python 程序,它可以用来读取 Python 2.x 版本的代码,并使用一系列的修复器来将其转换为合法的 Python 3.x 代码。标准库中已经包含了丰富的修复器,这足以处理绝大多数代码。不过 2to3 的支持库 lib2to3 是一个很灵活通用的库,所以你也可以为 2to3 编写你自己的修复器。lib2to3 也可以用在那些需要自动处理 Python 代码的应用中。

Converts the print statement to the print() function.
print 语句转换为 print() 函数。

Python 2.x codeprint 语句 (print statement)
Python 3.x codeprint() 函数 (print() function)

2. print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.
将 objects 打印到 file 指定的文本流,以 sep 分隔并在末尾加上 endsependfileflush 如果存在,它们必须以关键字参数的形式给出。

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
所有非关键字参数都会被转换为字符串,就像是执行了 str() 一样,并会被写入到流,以 sep 且在末尾加上 endsepend 都必须为字符串,它们也可以为 None,这意味着使用默认值。如果没有给出 objects,则 print() 将只写入 end。

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.
file 参数必须是一个具有 write(string) 方法的对象。如果参数不存在或为 None,则将使用 sys.stdout。由于要打印的参数会被转换为文本字符串,因此 print() 不能用于二进制模式的文件对象。对于这些对象,应改用 file.write(...)

Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.
输出是否被缓存通常决定于 file,但如果 flush 关键字参数为真值,流会被强制刷新。

第二个参数 sep,表示 objects 参数连接时使用的字符,默认是空格。
第四个参数 file,表示输出到哪里,默认是 sys.stdout
第五个参数 flush,表示是否立即输出到 file 所指定的对象中。当为 True 时,立即输出。当为 False 时,则取决于 file 对象 (一般不立即输出)。

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\foreverstrong>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("yongqiang", "cheng", sep='*')
yongqiang*cheng
>>> exit()

C:\Users\foreverstrong>

3. Python 2.x print 语句 (print statement) 不换行

print 语句句尾加上逗号实现不换行,默认换行。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def main():
    print "yong"
    print "qiang"
    print "cheng"

    print "yong",
    print "qiang",
    print "cheng"


if __name__ == "__main__":
    main()
/usr/bin/python2.7 /home/strong/darknet_work/darknet_20190629/darknet/yongqiang.py
yong
qiang
cheng
yong qiang cheng

Process finished with exit code 0

4. Python 3.x print() 函数 (print() function) 不换行

end 参数来指定结束时输出的字符,默认换行。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def main():
    print("yong")
    print("qiang")
    print("cheng")

    print("yong", end=" ")
    print("qiang", end=" ")
    print("cheng", end=" ")

    print()

    print("yong", end="")
    print("qiang", end="")
    print("cheng", end="")


if __name__ == "__main__":
    main()
/usr/bin/python3.5 /home/strong/darknet_work/darknet_20190629/darknet/yongqiang.py
yong
qiang
cheng
yong qiang cheng 
yongqiangcheng
Process finished with exit code 0

5. Python 2.x print 语句 (print statement) 不换行

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function


def main():
    print("yong")
    print("qiang")
    print("cheng")

    print("yong", end=" ")
    print("qiang", end=" ")
    print("cheng", end=" ")

    print()

    print("yong", end="")
    print("qiang", end="")
    print("cheng", end="")


if __name__ == "__main__":
    main()
/usr/bin/python2.7 /home/strong/darknet_work/darknet_20190629/darknet/yongqiang.py
yong
qiang
cheng
yong qiang cheng 
yongqiangcheng
Process finished with exit code 0

6. flush 参数

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time


def main():
    print("Loading", end="")

    for i in range(6):
        print(".", end='', flush=True)
        time.sleep(0.2)


if __name__ == "__main__":
    main()
/usr/bin/python3.5 /home/strong/darknet_work/darknet_20190629/darknet/yongqiang.py
Loading......
Process finished with exit code 0

flush 参数主要是刷新,默认 flush=False,不刷新。当 flush=True 时,它会立即把内容刷新输出。

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

### 查看 Python 内置函数的源代码 Python内置函数Built-in functions)是由 C 实现的核心部分,因此它们的源码主要位于 Python 解释器的 C 源代码中。以下是关于如何找到这些内置函数源代码的相关信息: #### 1. **C实现的位置** 大多数 Python 内置函数是在 C 中实现的,其源代码通常可以在 Python 官方仓库中的 `Modules/` 和 `Objects/` 文件夹下找到[^1]。例如: - `print()` 函数的功能实际上由 `_PySys_WriteStdout()` 或类似的底层方法支持,在 `Python/sysmodule.c` 中定义[^2]。 - `complex()` 类型及其构造函数可以通过查找 `Objects/complexobject.c` 来了解其实现细节[^3]。 #### 2. **具体例子:`print()` 函数** 对于像 `print()` 这样的内置函数,它的实际行为依赖于解释器内部机制以及标准库的支持。虽然表面上看起来像是普通的 Python 函数,但实际上它是通过 C API 调用完成工作的。具体的实现在 `bltinmodule.c` 文件中有体现,其中包含了大量核心功能的封装。 #### 3. **其他常用内置函数** 一些其他的内置函数也可以在其对应的模块或对象文件中追踪到: - `filter()` 的逻辑处理在 `itertools` 模块或者通用迭代工具集中有描述[^4]。 - 使用 gdb 工具调试时,可以借助命令 `(gdb) output func` 输出特定函数的信息,这有助于理解某些低级操作的具体位置。 #### 4. **访问方式** 要获取完整的源代码并研究这些内置函数的工作原理,可以从以下几个途径入手: - 下载官方发布的最新版本源码包 https://github.com/python/cpython- 浏览在线存储库 https://github.com/python/cpython/tree/main ,特别关注上述提到的关键目录路径。 ```bash git clone https://github.com/python/cpython.git cd cpython grep -r 'builtin_function_or_method' . ``` 此脚本可以帮助定位涉及 “built-in” 关键词的部分代码片段。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值