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

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

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
函数
139
9.11
名称中
_
__
的用法
以双下划线(
__
)起止的名称是保留给
Python
内部使用的,不要将它们与你自己的变量一
起使用。之所以选择这种命名形式,是因为应用开发人员不大可能会选择双下划线作为变
量名称。
例如,系统变量
function.__name__
中保存着函数名称,
function.__doc__
中保存着文档字
符串:
>>> def amazing():
... '''This is the amazing function.
... Want to see it again?'''
... print('This function is named:', amazing.__name__)
... print('And its docstring is:', amazing.__doc__)
...
>>> amazing()
This function is named: amazing
And its docstring is: This is the amazing function.
Want to see it again?
如同先前
globals()
的输出所示,主程序被赋予特殊名称
__main__
9.12
递归
到目前为止,我们调用的函数都是直接执行操作,或是再调用其他函数。但如果一个函数
调用自身呢?
9
这叫作
递归
。就像用
while
for
弄出来的死循环,你并不想搞个无穷递归
出来。难道还得去操心时空裂缝吗?
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