Skip to Content
Mastering Python Design Patterns - Second Edition
book

Mastering Python Design Patterns - Second Edition

by Kamon Ayeva, Sakis Kasampalis
August 2018
Intermediate to advanced
248 pages
5h 51m
English
Packt Publishing
Content preview from Mastering Python Design Patterns - Second Edition

Implementation

To demonstrate the proxy pattern, we will implement a simple protection proxy to view and add users. The service provides two options:

  • Viewing the list of users: This operation does not require special privileges
  • Adding a new user: This operation requires the client to provide a special secret message

The SensitiveInfo class contains the information that we want to protect. The users variable is the list of existing users. The read() method prints the list of the users. The add() method adds a new user to the list.

Let's consider the following code:

class SensitiveInfo:     def __init__(self):         self.users = ['nick', 'tom', 'ben', 'mike']       def read(self):         nb = len(self.users) print(f"There are {nb} users: {' '.join(self.users)}") ...
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 Design Patterns - Second Edition - Second Edition

Learning Python Design Patterns - Second Edition - Second Edition

Chetan Giridhar, Gennadiy Zlobin

Publisher Resources

ISBN: 9781788837484Supplemental Content