OOP Decarators and Iterators Flashcards
(11 cards)
_______ are an extremely powerful concept in Python. They allow you to modify the behavior of a function without changing the code of the function itself.
Decarators
Simple decarator function
def decorator(func):
def wrapper(): print("Before calling the function.") func() print("After calling the function.") return wrapper
Applying the decorator to a function
@decorator
def greet():
print(“Hello, World!”)
greet()
def decorator(func):
def wrapper(): print("Before calling the function.") func() print("After calling the function.") return wrapper
Applying the decorator to a function
@decorator
def greet():
print(“Hello, World!”)
greet()
What does this do?
decorator takes the greet function as an argument.
It returns a new function (wrapper) that first prints a message, calls greet() and then prints another message.
The @decorator syntax is a shorthand for greet = decorator(greet).
Syntax of decarators
def decorator_name(func):
def wrapper(args, **kwargs):
# Add functionality before the original function call
result = func(args, **kwargs)
# Add functionality after the original function call
return result
return wrapper
@decorator_name
def function_to_decorate():
# Original function code
pass
- decorator_name(func):
- decorator_name: This is the name of the decorator function.
- func: This parameter represents the function being decorated. When you use a decorator, the decorated function is passed to this parameter.
- wrapper(*args, **kwargs):
wrapper: This is a nested function inside the decorator. It wraps the original function, adding additional functionality.
*args: This collects any positional arguments passed to the decorated function into a tuple.
**kwargs: This collects any keyword arguments passed to the decorated function into a dictionary.
The wrapper function allows the decorator to handle functions with any number and types of arguments.
- @decorator_name:
This syntax applies the decorator to the function_to_decorate function. It is equivalent to writing function_to_decorate = decorator_name(function_to_decorate)
Can we apply multiple decaratrs to a function
yes
Key Properties of Higher-Order Functions:
Taking functions as arguments: A higher-order function can accept other functions as parameters.
Returning functions: A higher-order function can return a new function that can be called later.
In Python, functions are _____-______ objects, meaning that they can be treated like any other object, such as integers, strings, or lists. This gives functions a unique level of flexibility and allows them to be passed around and manipulated in ways that are not possible in many other programming languages.
first class
What Does It Mean for Functions to Be First-Class Objects?
Can be assigned to variables: Functions can be assigned to variables and used just like any other value.
Can be passed as arguments: Functions can be passed as arguments to other functions.
Can be returned from other functions: Functions can return other functions, which is a key concept in decorators.
Can be stored in data structures: Functions can be stored in lists, dictionaries, or other data structures.