Skip to Content
Learning Python, 3rd Edition
book

Learning Python, 3rd Edition

by Mark Lutz
October 2007
Beginner
752 pages
19h 44m
English
O'Reilly Media, Inc.
Content preview from Learning Python, 3rd Edition

Chapter 21. Advanced Module Topics

This chapter concludes Part V with a collection of more advanced module-related topics—relative import syntax, data hiding, the _ _future_ _ module, the _ _name_ _ variable, sys.path changes, and so on—along with the standard set of gotchas and exercises related to what we’ve covered in this part of the book. Like functions, modules are more effective when their interfaces are well defined, so this chapter also briefly reviews module design concepts, some of which we have explored in prior chapters.

Despite the word “advanced” in this chapter’s title, some of the topics discussed here (such as the _ _name_ _ trick) are widely used, so be sure you take a look before moving on to classes in the next part of the book.

Data Hiding in Modules

As we’ve seen, a Python module exports all the names assigned at the top level of its file. There is no notion of declaring which names should and shouldn’t be visible outside the module. In fact, there’s no way to prevent a client from changing names inside a module if it wants to.

In Python, data hiding in modules is a convention, not a syntactical constraint. If you want to break a module by trashing its names, you can, but fortunately, I’ve yet to meet a programmer who would. Some purists object to this liberal attitude toward data hiding, and claim that it means Python can’t implement encapsulation. However, encapsulation in Python is more about packaging than about restricting.

Minimizing from * Damage: _X and ...

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

Learning Python, 6th Edition

Learning Python, 6th Edition

Mark Lutz
Learning Python, 2nd Edition

Learning Python, 2nd Edition

Mark Lutz, David Ascher

Publisher Resources

ISBN: 9780596513986Purchase bookSupplemental ContentErrata Page