Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
Python语言及其应用(第2版)
by
Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from
Python语言及其应用(第2版)
170
第
11
章
模块、包和赠品
到目前为止,我们从内建数据类型学到了构建越来越大的数据和代码结构。在本章,你将
学会如何编写真实完整的
Python
程序。你将编写自己的
模块
,并学会如何使用来自
Python
标准库和其他源的模块。
本书正文是按照单词、句子、段落和章节的层次结构来组织的。否则的话,很快就让人读
不下去了。
1
代码也是按类似的自下而上的结构来组织的:数据类型就像单词,表达式和语
句就像句子,函数就像段落,模块就像章节。依照这种类比,在本书中,当我说会在第
8
章
解释某件事情的时候,就类似于编程中引用另一个模块的代码。
11.1
模块和
import
语句
本书会在不止一个文件中创建和使用
Python
代码。
模块
就是包含任意
Python
代码的文件。
你不用做什么特别的事——任何
Python
代码都可以被其他人作为模块使用。
我们使用
Python
的
import
语句引用其他模块的代码。这使得被导入模块中的代码和变量
可供我们自己的程序使用。
11.1.1
导入模块
import
语句最简单的用法是
import
module
,其中
module
是另一个
Python
文件的名称(不
包含
.py
扩展名)
。
比方说,你和其他几个人想在午餐时点快餐,但又不想长时间讨论,而你最终总是选择声
音最大的那个人想吃的东西。还是让计算机来决定吧!下面来编写一个模块,其中包含可
注
1
:
至少,比现在的可读性要差一点儿。
模块、包和赠品
|
171
以随机返回一款快餐的函数,以及负责调用该函数并打印出选择结果的主程序。
这个模块(
fast.py
)如例
1
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编程入门与实战
Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南
Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程
保罗·J·戴特尔
Python面向对象编程指南
Posts & Telecom Press, Steven F. Lott
Publisher Resources
ISBN: 9787115586223