Skip to Content
Python数据处理
book

Python数据处理

by Jacqueline Kazil, Katharine Jarmul
July 2017
Intermediate to advanced
398 pages
11h 54m
Chinese
Posts & Telecom Press
Content preview from Python数据处理
368
附录
E
随着你的代码变得更高级和复杂,问题会变得越来越难办。由于你的代码更加抽象,你会
将所有的代码保存在变量中。如果一个函数或方法返回一个不符预期的类型(例如
None
不是列表),这个对象可能被传递到另一个函数中——可能是一个不接受
None
类型的函数,
之后抛出一个错误。很可能错误被捕获了,但代码会认为异常是因为另外的问题触发的,
并且继续执行。这会很快地脱离你的控制,并且变成一个相当难以调试的问题。
对于如何处理这些问题,最好的建议是编写非常精准又清晰的代码。你应该积极测试你的
代码(确保没有
bug
),持续关注你的脚本,并注意任何反常的行为,以此确保函数永远返
回期待的内容。你还需要添加日志来帮助确认对象包含的内容。除此之外,清楚你捕获的
异常,而不只是捕获所有的异常,这会帮助你更容易地找到和修复问题。
最后,有时
Python
会实现
PEP-484
https://www.python.org/dev/peps/pep-0484/
),它包含了
类型提示,允许你检查传递的变量和代码,以自我检查这些问题。这在未来
Python 3
发布
之前可能不会被合并,但是好消息是,这已经在进行当中,你可以期待在未来看到更多的
有关类型检查的结构。
E.9
 捕获多个异常
随着代码的发展,你会想要在同一行代码中捕获多个异常。举个例子,你可能想要捕获一
TypeError
,同时还有
AttributeError
。如果你以为传递的是一个字典,而实际上传递的
是一个列表,可能就是这种情况。它可能有一些相同的属性,但是不是所有属性。如果你 ...
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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据科学中的实用统计学(第2版)

数据科学中的实用统计学(第2版)

Peter Bruce, Andrew Bruce, Peter Gedeck
Java持续交付

Java持续交付

Daniel Bryant, Abraham Marín-Pérez
解密金融数据

解密金融数据

Justin Pauley

Publisher Resources

ISBN: 9787115459190