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版)
176
11
critters
rougarou.py
如果
north
south
均位于模块搜索路径内,则可以导入这些模块,就好像它们仍然位于单
一目录的包中那样。
from critters import wendigo, rougarou
11.2.4
模块和对象
什么时候该把代码放入模块?什么时候该把代码放入对象?
两者在许多方面看起来很相似。对于包含名为
stuff
的内部数据的对象或模块
thing
,其
数据可以使用
thing.stuff
访问。
stuff
可以在创建模块或类时定义,也可以随后赋值。
模块中的所有类、函数和全局变量都可以在外部访问。对象可以使用属性和双下划线命名
方式隐藏或控制对于其数据特性的访问。
这意味着可以这样做:
>>> import math
>>> math.pi
3.141592653589793
>>> math.pi = 3.0
>>> math.pi
3.0
我们刚刚是把这台计算机上的数学工具毁掉了吗?不是。这不会影响
Python
math
模块。
我们只是修改了程序所导入的
math
模块代码副本中的
pi
值,程序结束之后,一切改动就
都消失无踪了。
程序导入的模块只有一份副本,即便多次导入。你可以用它来保存导入该模块的代码感兴
趣的全局内容。这就像是类,同样只有一份副本,但可以由此创建多个对象。
11.3 Python
标准库
Python
的著名主张之一是“自备电池”
2
——
一个庞大的标准模块库,可用于完成大量实用
任务。标准库被分开存放,以免造成核心语言臃肿。当你着手编写 ...
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

可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
量子计算机编程:从入门到实践

量子计算机编程:从入门到实践

Eric R. Johnston, Nicholas Harrigan, Mercedes Gimeno-Segovia
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787115586223