From 8a9ef25e5cd8ca39b8ae66fff7cf07ddd449ddec Mon Sep 17 00:00:00 2001 From: Seth Bradford Wagenman <11823408+fPkX6F1nGTX@users.noreply.github.com> Date: Fri, 10 Jan 2025 07:49:53 -0500 Subject: [PATCH 1/4] Add example of class method self-decoration --- docs/cheatsheet/decorators.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/docs/cheatsheet/decorators.md b/docs/cheatsheet/decorators.md index c3496081..512c9865 100644 --- a/docs/cheatsheet/decorators.md +++ b/docs/cheatsheet/decorators.md @@ -102,6 +102,39 @@ def foo(bar): ## Class based decorators +To decorate a class methos, you must define the decorator within the class. When only the implicit argument `self` is passed to the method, without any other additional arguments, you must make a separate decorator for only those methods without any additional arguments. An example of this is when you want to catch and print exceptions in a certain way. + +```python +class DecorateMyMethod: + + def decorator_for_class_method(method): + def wrapper_for_class_method(self, *args, **kwargs) + try: + return method(self, *args, **kwargs) + except Exception as e: + print("\nWARNING: Please make note of the following:\n") + print(e) + return wrapper_for_class_method + + def __init__(self,succeed:bool): + self.succeed = succeed + + @wrapper_for_class_method + def class_action(self): + if self.succeed: + print("You succeeded by choice.") + else: + raise Exception("Epic fail of your own creation.") + +test_succeed = DecorateMyMethods(True) +test_succeed.class_action() +# You succeeded by choice. + +test_fail = DecorateMyMethod(False) +test_fail.class_action() +# Exception: Epic fail of your own creation. +``` + A decorator can also be defined as a class instead of a method. This is useful for maintaining and updating a state, such as in the following example, where we count the number of calls made to a method: ```python From c856491a87720ba57954c7f74494522113b2a291 Mon Sep 17 00:00:00 2001 From: Seth Bradford Wagenman <11823408+fPkX6F1nGTX@users.noreply.github.com> Date: Fri, 10 Jan 2025 07:52:34 -0500 Subject: [PATCH 2/4] Corrected erroneous example --- docs/cheatsheet/decorators.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/cheatsheet/decorators.md b/docs/cheatsheet/decorators.md index 512c9865..ea8e2ef1 100644 --- a/docs/cheatsheet/decorators.md +++ b/docs/cheatsheet/decorators.md @@ -107,10 +107,10 @@ To decorate a class methos, you must define the decorator within the class. When ```python class DecorateMyMethod: - def decorator_for_class_method(method): - def wrapper_for_class_method(self, *args, **kwargs) + def decorator_for_class_method_with_no_args(method): + def wrapper_for_class_method(self) try: - return method(self, *args, **kwargs) + return method(self) except Exception as e: print("\nWARNING: Please make note of the following:\n") print(e) @@ -119,7 +119,7 @@ class DecorateMyMethod: def __init__(self,succeed:bool): self.succeed = succeed - @wrapper_for_class_method + @decorator_for_class_method_with_no_args def class_action(self): if self.succeed: print("You succeeded by choice.") From 1482fa5e98a0306ed6fea16c41e733b17536110a Mon Sep 17 00:00:00 2001 From: Seth Bradford Wagenman <11823408+fPkX6F1nGTX@users.noreply.github.com> Date: Fri, 10 Jan 2025 08:05:45 -0500 Subject: [PATCH 3/4] Made all lines less than 80 characters in length --- docs/cheatsheet/decorators.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/cheatsheet/decorators.md b/docs/cheatsheet/decorators.md index ea8e2ef1..382cb87d 100644 --- a/docs/cheatsheet/decorators.md +++ b/docs/cheatsheet/decorators.md @@ -1,17 +1,21 @@ --- title: Python Decorators - Python Cheatsheet -description: A Python Decorator is a syntax that provide a concise and reusable way for extending a function or a class. +description: A Python Decorator is a syntax that provide a concise +and reusable way for extending a function or a class. --- Python Decorators -A Python Decorator provides a concise and reusable way for extending a function or a class. +A Python Decorator provides a concise and reusable way for extending +a function or a class. ## Bare bone decorator -A decorator in its simplest form is a function that takes another function as an argument and returns a wrapper. The following example shows the creation of a decorator and its usage. +A decorator in its simplest form is a function that takes another +function as an argument and returns a wrapper. The following example +shows the creation of a decorator and its usage. ```python def your_decorator(func): @@ -58,7 +62,8 @@ foo("Jack") ## Template for a basic decorator -This template is useful for most decorator use-cases. It is valid for functions with or without parameters, and with or without a return value. +This template is useful for most decorator use-cases. It is valid for functions +with or without parameters, and with or without a return value. ```python import functools @@ -102,7 +107,11 @@ def foo(bar): ## Class based decorators -To decorate a class methos, you must define the decorator within the class. When only the implicit argument `self` is passed to the method, without any other additional arguments, you must make a separate decorator for only those methods without any additional arguments. An example of this is when you want to catch and print exceptions in a certain way. +To decorate a class methos, you must define the decorator within the class. When +only the implicit argument `self` is passed to the method, without any other +additional arguments, you must make a separate decorator for only those methods +without any additional arguments. An example of this is when you want to catch +and print exceptions in a certain way. ```python class DecorateMyMethod: @@ -135,7 +144,9 @@ test_fail.class_action() # Exception: Epic fail of your own creation. ``` -A decorator can also be defined as a class instead of a method. This is useful for maintaining and updating a state, such as in the following example, where we count the number of calls made to a method: +A decorator can also be defined as a class instead of a method. This is useful +for maintaining and updating a state, such as in the following example, where we +count the number of calls made to a method: ```python class CountCallNumber: From 369ab47784e1213a364ad21db7e12bc5fa5017d8 Mon Sep 17 00:00:00 2001 From: Seth Bradford Wagenman <11823408+fPkX6F1nGTX@users.noreply.github.com> Date: Fri, 10 Jan 2025 08:06:47 -0500 Subject: [PATCH 4/4] Made all lines less than or equal to 80 characters long --- docs/cheatsheet/decorators.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/cheatsheet/decorators.md b/docs/cheatsheet/decorators.md index 382cb87d..ea1ee5ec 100644 --- a/docs/cheatsheet/decorators.md +++ b/docs/cheatsheet/decorators.md @@ -1,7 +1,6 @@ --- title: Python Decorators - Python Cheatsheet -description: A Python Decorator is a syntax that provide a concise -and reusable way for extending a function or a class. +description: A Python Decorator is a syntax that provide a concise and reusable way for extending a function or a class. ---