Skip to Content
Expert Python Programming - Third Edition
book

Expert Python Programming - Third Edition

by Michał Jaworski, Tarek Ziadé, Cody Jackson
April 2019
Intermediate to advanced
646 pages
16h 48m
English
Packt Publishing
Content preview from Expert Python Programming - Third Edition

Decorators

Decorators were added in Python to make function and method wrapping easier to read and understand. A decorator is simply any function that receives a function and returns an enhanced one. The original use case was to be able to define the methods as class methods or static methods on the head of their definition. Without the decorator syntax, it would require a rather sparse and repetitive definition:

class WithoutDecorators: 
    def some_static_method(): 
        print("this is static method") 
    some_static_method = staticmethod(some_static_method) 
     
    def some_class_method(cls): 
        print("this is class method") 
    some_class_method = classmethod(some_class_method) 

Dedicated decorator syntax code is shorter and easier to understand:

class WithDecorators: ...
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

Expert Python Programming - Fourth Edition

Expert Python Programming - Fourth Edition

Michał Jaworski, Tarek Ziade, Tarek Ziadé

Publisher Resources

ISBN: 9781789808896Other