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版)
函数
143
>>> for word in words:
... if word.isupper():
... raise UppercaseException(word)
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
__main__.UppercaseException: MO
我们甚至都没有定义
UppercaseException
的任何操作(只是使用了
pass
),在出现异常时,
由其父类
Exception
负责输出错误消息。
你可以访问异常对象并打印出其内容。
>>> try:
... raise OopsException('panic')
... except OopsException as exc:
... print(exc)
...
panic
9.15
后续内容
对象!在一本关于面向对象语言的书中,这是一个避不开的主题。
9.16
练习
1.
定义函数
good()
,返回列表:
['Harry', 'Ron', 'Hermione']
2.
定义生成器函数
get_odds()
,返回
range(10)
中的奇数。使用
for
循环查找并打印出第
3
个返回值。
3.
定义装饰器
test
,在调用函数时打印出
'start'
,函数结束时打印出
'end'
4.
定义异常
OopsException
。产生该异常,看看会发生什么。然后,编写代码,捕获该异
常并打印出
'Caught an oops'
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