Serious Python by Julien Danjou
Author:Julien Danjou
Language: eng
Format: epub, azw3, pdf
Publisher: No Starch Press
Published: 2018-12-31T16:00:00+00:00
wraps: A Decorator for Decorators
It can get tedious to use update_wrapper() manually when creating decorators, so functools provides a decorator for decorators called wraps. Listing 7-6 shows the wraps decorator in use.
import functools
def check_is_admin(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
if kwargs.get('username') != 'admin':
raise Exception("This user is not allowed to get food")
return f(*args, **kwargs)
return wrapper
class Store(object):
@check_is_admin
def get_food(self, username, food):
"""Get food from storage."""
return self.storage.get(food)
Listing 7-6: Updating our decorator with wraps from functools
With functools.wrap, the decorator function check_is_admin() that returns the wrapper() function takes care of copying the docstring, name function, and other information from the function f passed as argument. Thus, the decorated function (get_food(), in this case) still sees its unchanged signature.
Download
Serious Python by Julien Danjou.azw3
Serious Python by Julien Danjou.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7784)
Grails in Action by Glen Smith Peter Ledbrook(7699)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6420)
Kotlin in Action by Dmitry Jemerov(5068)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3812)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3334)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(3088)
Mastering Bitcoin: Programming the Open Blockchain by Andreas M. Antonopoulos(2870)
The Art Of Deception by Kevin Mitnick(2609)
Drugs Unlimited by Mike Power(2468)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2315)
The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution by Walter Isaacson(2312)
Writing for the Web: Creating Compelling Web Content Using Words, Pictures and Sound (Eva Spring's Library) by Lynda Felder(2263)
SEO 2018: Learn search engine optimization with smart internet marketing strategies by Adam Clarke(2194)
A Blueprint for Production-Ready Web Applications: Leverage industry best practices to create complete web apps with Python, TypeScript, and AWS by Dr. Philip Jones(2194)
JavaScript by Example by S Dani Akash(2138)
DarkMarket by Misha Glenny(2084)
Wireless Hacking 101 by Karina Astudillo(2079)
Full-Stack React Projects by Shama Hoque(1991)
