![]() |
VOOZH | about |
Decorators are flexible way to modify or extend behavior of functions or methods, without changing their actual code.
Example: Below is an example to demonstrate how decorator functions:
Before calling the function. Hello, World! After calling the function.
Explanation:
Decorators often need to work with functions that have arguments. We use *args and **kwargs so our wrapper can accept any number of arguments.
Example: Let's see an example of a decorator that adds functionality before and after a function call.
Before execution After execution 8
Explanation:
Functions are first-class objects, meaning they can be treated like any other object (such as integers, strings or lists). This allows functions to be assigned to variables, passed as arguments, returned from other functions and stored in data structures, including decorators.
Example: This code demonstrates all four properties of functions as first-class objects.
Hello, Alex! Hello, Elon! 10
Explanation:
Role of First-Class Functions in Decorators:
Higher-order functions are functions that either take other functions as input, return a function as output, or both. They allow functions to be treated like data, making code more flexible and reusable.
Example: This code shows a higher-order function that takes another function as an argument and applies it to a given value.
25
Explanation: fun is a higher-order function because it takes another function f as an argument and applies it to the value x.
Role of Higher-Order Functions in Decorators: Decorators are higher-order functions that take a function, modify it and return a new one with extended or altered behavior.
1. Function Decorators: used to wrap and enhance functions by adding extra behavior before or after the original function runs.
Example: In this Example, a decorator prints a message before and after executing wrapped function.
>>> Starting function Hello, World! >>> Function finished
Explanation:
2. Method Decorators: Special decorators used for methods inside a class. They work like function decorators but handle the self parameter for instance methods.
Example: Here, a decorator prints a message before and after a method is executed, while correctly handling self argument.
Before method execution Hello! After method execution
Explanation:
3. Class Decorators: used to modify or enhance behavior of a class. Like function decorators, class decorators are applied to class definition. They work by taking class as an argument and returning a modified version of class.
Example: This code demonstrates a class decorator that adds a class_name attribute to a class, storing class’s name.
Person
Explanation:
1. @staticmethod: used to define a method that doesn't operate on an instance of class (i.e., it doesn't use self). Static methods are called on class itself, not on an instance of class.
Example: This example shows how to define and use a @staticmethod inside a class.
8
Explanation:
2. @classmethod: used to define a method that operates on class itself (i.e., it uses cls). Class methods can access and modify class state that applies across all instances of class.
Example: This code defines a class Employee with a class variable raise_amount and a class method set_raise_amount that updates this variable for entire class.
1.1
Explanation:
3. @property: used to define a method as a property, which allows to access it like an attribute. This is useful for encapsulating implementation of a method while still providing a simple interface.
Example: This code defines a circle class demonstrating @property for controlled attribute access, allowing safe updates to radius.
5 78.53975 314.159
Explanation:
Chaining decorators means applying multiple decorators to same function. Each decorator wraps function in sequence, adding layered behavior.
Example: This example shows how chaining decorators works by applying two decorators in different orders to see how output changes.
400 200
Explanation: