Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced content levelIntermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
函数
129
你可以创建非常长的文档字符串,甚至加入丰富的格式:
def print_if_true(thing, check):
'''
Prints the first argument if a second argument is true.
The operation is:
1. Check whether the *second* argument is true.
2. If it is, print the *first* argument.
'''
if check:
print(thing)
要想打印出函数的文档字符串,调用
Python
help()
函数即可。传入函数名称,就可以
得到参数列表以及格式整齐的文档字符串:
>>> help(echo)
Help on function echo in module __main__:
echo(anything)
echo returns its input argument
如果你只想查看未经格式化的原始文档字符串,可以运行以下代码:
>>> print(echo.__doc__)
echo returns its input argument
样子古怪的
__doc__
是文档字符串作为函数变量的内部名称。双下划线(在
Python
行话中
又叫作
dunder
8
)在很多地方被用于命名
Python
内部变量,因为程序员不太可能在自己的变
量名中使用双下划线。
9.5
作为
头等公民
的函数
Python
中,一切皆为 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程

Python技术基础视频教程

保罗·J·戴特尔
Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott

Publisher Resources

ISBN: 9787115586223