10.5 Defining and using decorators
We’ve used Python decorators several times so far, but it’s time we learned how to code them ourselves. We saw the decorators
-
@abstractmethod
in section 7.13.2 to indicate a method from an abstract base class that coders must override -
@classmethod
and@staticmethod
in section 7.12 to define class and static methods -
@property
in section 7.5 to define properties from parameter-less class instance methods
Here’s the core idea with decorators: given a function my_function
, I want to
wrap it with additional code that calls my_function
. I’m decorating the
outside of my_function
with extra capability. For extra credit, I want to use
my_function
to access my original code with the new decorations.
I’ll begin with a simple function that prints 100
.
def one_hundred...