OOP Decarators and Iterators Flashcards

(11 cards)

1
Q

_______ 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.

A

Decarators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Simple decarator function

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Syntax of decarators

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. decorator_name(func):
A
  1. decorator_name: This is the name of the decorator function.
  2. func: This parameter represents the function being decorated. When you use a decorator, the decorated function is passed to this parameter.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. wrapper(*args, **kwargs):
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. @decorator_name:
A

This syntax applies the decorator to the function_to_decorate function. It is equivalent to writing function_to_decorate = decorator_name(function_to_decorate)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can we apply multiple decaratrs to a function

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Key Properties of Higher-Order Functions:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

first class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What Does It Mean for Functions to Be First-Class Objects?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly