Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Modular Programming with Python

You're reading from   Modular Programming with Python Introducing modular techniques for building sophisticated programs using Python

Arrow left icon
Product type Paperback
Published in May 2016
Publisher Packt
ISBN-13 9781785884481
Length 246 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Erik Westra Erik Westra
Author Profile Icon Erik Westra
Erik Westra
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Introducing Modular Programming 2. Writing Your First Modular Program FREE CHAPTER 3. Using Modules and Packages 4. Using Modules for Real-World Programming 5. Working with Module Patterns 6. Creating Reusable Modules 7. Advanced Module Techniques 8. Testing and Deploying Modules 9. Modular Programming as a Foundation for Good Programming Technique Index

Encapsulation


Encapsulation is another programming pattern that often applies to modules and packages. Using encapsulation, you have a thing—for example, a color, a customer, or a currency—that you need to store data about, but you hide the representation of this data from the rest of your system. Rather than make the thing available directly, you provide functions for setting, retrieving, and manipulating the thing's data.

To see how this works, let's look back at a module we wrote in the previous chapter. Our chart.py module lets the user define a chart and set the various pieces of information about it. Here is a copy of the code that we wrote for this module:

def new_chart():
    return {}

def set_title(chart, title):
    chart['title'] = title

def set_x_axis(chart, x_axis):
    chart['x_axis'] = x_axis

def set_y_axis(chart, minimum, maximum, labels):
    chart['y_min']    = minimum
    chart['y_max']    = maximum
    chart['y_labels'] = labels

def set_series_type(chart, series_type...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime