Reader small image

You're reading from  Advanced Python Programming - Second Edition

Product typeBook
Published inMar 2022
PublisherPackt
ISBN-139781801814010
Edition2nd Edition
Right arrow
Author (1)
Quan Nguyen
Quan Nguyen
author image
Quan Nguyen

Quan Nguyen is a Python programmer and machine learning enthusiast. He is interested in solving decision-making problems under uncertainty. Quan has authored several books on Python programming and scientific computing. He is currently pursuing a Ph.D. degree in computer science at Washington University in St. Louis, researching Bayesian methods in machine learning.
Read more about Quan Nguyen

Right arrow

Real-world examples

The decorator pattern is generally used for extending the functionality of an object. In everyday life, examples of functionality extensions are adding a holder stand to a phone or using different camera lenses.

In the Django framework, which uses decorators a lot, we have the View decorators, which can be used for the following (j.mp/djangodec):

  • Restricting access to views based on the HTTP request
  • Controlling the caching behavior on specific views
  • Controlling compression on a per-view basis
  • Controlling caching based on specific HTTP request headers

Both the Pyramid framework and the Zope application server also use decorators to achieve various goals, such as the following:

  • Registering a function as an event subscriber
  • Protecting a method with a specific permission
  • Implementing the adapter pattern

To be more concrete, we will iterate the specific use cases of the design pattern in the next section.

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Advanced Python Programming - Second Edition
Published in: Mar 2022Publisher: PacktISBN-13: 9781801814010

Author (1)

author image
Quan Nguyen

Quan Nguyen is a Python programmer and machine learning enthusiast. He is interested in solving decision-making problems under uncertainty. Quan has authored several books on Python programming and scientific computing. He is currently pursuing a Ph.D. degree in computer science at Washington University in St. Louis, researching Bayesian methods in machine learning.
Read more about Quan Nguyen