Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit c045a25

Browse files
committed
Added multi-decorator description and examples
Signed-off-by: Serhii Horodilov <sgorodil@gmail.com>
1 parent cd3ef0c commit c045a25

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/basics/decorators.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,62 @@ exact the same things as the first decorator example:
192192

193193
There is no way to *undecorate* object in Python. Once something is bound
194194
to the decorator's wrapper - it is decorated forever.
195+
196+
A decorated function still remains a **function**. So, it can be decorated
197+
once more time again, and again, and again...
198+
199+
.. code-block:: python
200+
201+
from typing import Callable
202+
203+
204+
def bread(func: Callable) -> Callable:
205+
def wrapper():
206+
print("<--bread-->")
207+
func()
208+
print("<--bread-->")
209+
210+
return wrapper
211+
212+
213+
def vegetables(func: Callable) -> Callable:
214+
def wrapper():
215+
print("~~~salad~~~")
216+
print("***tomato***")
217+
func()
218+
219+
return wrapper
220+
221+
222+
def cheese(func: Callable) -> Callable:
223+
def wrapper():
224+
func()
225+
print("---cheese---")
226+
227+
return wrapper
228+
229+
230+
@bread
231+
@vegetables
232+
@cheese
233+
def sandwich():
234+
print("_sliced_meat_")
235+
236+
.. code-block::
237+
238+
>>> sandwich()
239+
<--bread-->
240+
~~~salad~~~
241+
***tomato***
242+
_sliced_meat_
243+
---cheese---
244+
<--bread-->
245+
246+
.. note::
247+
248+
"Wrapper" is the alternative nickname for the Decorator pattern that
249+
clearly expresses the main idea of the pattern. A wrapper is an object that
250+
can be linked with some target object. The wrapper contains the same set of
251+
methods as the target and delegates to it all requests it receives.
252+
However, the wrapper may alter the result by doing something either before
253+
or after it passes the request to the target.

0 commit comments

Comments
 (0)