预计阅读本页时间:-
Classes Versus Modules
Let’s wrap up this chapter by briefly comparing the topics of this book’s last two parts: modules and classes. Because they’re both about namespaces, the distinction can be confusing. In short:
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
- Modules
- Are data/logic packages
- Are created by writing Python files or C extensions
- Are used by being imported
- Classes
- Implement new objects
- Are created by class statements
- Are used by being called
- Always live within a module
Classes also support extra features that modules don’t, such as operator overloading, multiple instance generation, and inheritance. Although both classes and modules are namespaces, you should be able to tell by now that they are very different things.