- Modular Programming with Python
- Erik Westra
- 475字
- 2021-07-16 10:47:16
Using modules and packages to organize a program
Modules and packages aren't just there to spread your Python code across multiple source files and directories—they allow you to organize your code to reflect the logical structure of what your program is trying to do. For example, imagine that you have been asked to create a web application to store and report on university examination results. Thinking about the business requirements that you have been given, you come up with the following overall structure for your application:

The program is broken into two main parts: a web interface, which interacts with the user (and with other computer programs via an API), and a backend, which handles the internal logic of storing information in a database, generating reports, and e-mailing results to students. As you can see, the web interface itself has been broken down into four parts:
- A user authentication section, which handles user sign-up, sign-in, and sign-out
- A web interface to view and enter exam results
- A web interface to generate reports
- An API, which allows other systems to retrieve exam results on request
As you consider each logical component of your application (that is, each of the boxes in the preceding illustration), you are also starting to think about the functionality that each component will provide. As you do this, you are already thinking in modular terms. Indeed, each of the logical components of your application can be directly implemented as a Python module or package. For example, you might choose to break your program into two main packages named web
and backend
, where:
- The
web
package has modules namedauthentication
,results
,reports
, andapi
- The
backend
package has modules nameddatabase
,reportgenerator
, andemailer
As you can see, each shaded box in the preceding illustration becomes a Python module, and each of the groupings of boxes becomes a Python package.
Once you have decided on the collection of packages and modules that you want to define, you can start to implement each component by writing the appropriate set of functions within each module. For example, the backend.database
module might have a function named get_students_results()
, which returns a single student's exam results for a given subject and year.
Note
In a real web application, your modular structure may actually be somewhat different. This is because you typically create a web application using a web application framework such as Django, which imposes its own structure on your program. However, in this example we are keeping the modular structure as simple as possible to show how business functionality translates directly into packages and modules.
Obviously, this example is fictitious, but it shows how you can think about a complex program in modular terms, breaking it down into individual components and then using Python modules and packages to implement each of these components in turn.
- Visual Basic 6.0程序設計計算機組裝與維修
- Vue.js 2 and Bootstrap 4 Web Development
- Rake Task Management Essentials
- R語言游戲數據分析與挖掘
- C語言最佳實踐
- UI智能化與前端智能化:工程技術、實現方法與編程思想
- Python貝葉斯分析(第2版)
- Python機器學習經典實例
- 大學計算機基礎實驗指導
- Java設計模式深入研究
- Python數據預處理技術與實踐
- JavaWeb從入門到精通(視頻實戰版)
- Swift Essentials(Second Edition)
- 軟件再工程:優化現有軟件系統的方法與最佳實踐
- Selenium Essentials