Advanced Functions Flashcards

(21 cards)

1
Q

What is a lambda function in Python?

A

An anonymous function defined as: lambda arguments: expression. Supports multiple arguments but only one expression.

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

Where are lambda functions commonly used?

A

As short, one-time functions for map(), filter(), reduce(), or sorting with key parameter.

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

How to sort a list of tuples by second element using lambda?

A

people.sort(key=lambda x: x[1])

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

What is a key advantage of lambdas over regular functions?

A

Conciseness when a full def function would be overkill.

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

Can all functions be converted to lambdas?

A

No, lambdas are limited to a single expression.

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

What is a callable in Python?

A

Any object that can be called like a function, e.g., functions, methods, classes, or instances with __call__ defined.

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

How to make a class instance callable?

A

Define a __call__(self, …) method in the class.

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

Why use callable objects?

A

To store state/configuration and allow simpler function-like syntax while preserving object data.

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

What is a decorator?

A

A function that takes another function and returns a modified version, allowing behavior to be added before and/or after the wrapped function runs.

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

Provide the basic syntax of a decorator.

A

def decorator(func):
def wrapper(args, **kwargs):
# code before
result = func(
args, **kwargs)
# code after
return result
return wrapper

@decorator
def my_func(): …

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

What is @decorator syntactic sugar for?

A

my_func = decorator(my_func)

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

Give an example of a logging decorator.

A

def log_decorator(func):
def wrapper(args, **kwargs):
print(f”Starting {func.__name__}”)
result = func(
args, **kwargs)
print(f”Ending {func.__name__}”)
return result
return wrapper

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

Name some built-in decorators in Python.

A

@staticmethod, @classmethod, @property

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

Which functools decorators are particularly useful?

A

@cache (caches results), @wraps (preserves metadata), @singledispatch (type-based function overloading).

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

What are asynchronous functions?

A

Functions declared with async def that can be paused and resumed, allowing concurrent, non-blocking execution.

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

What is a coroutine?

A

A special function (async def) that can pause (await) and resume, yielding control back to the event loop.

17
Q

What keyword is used inside coroutines to pause execution?

18
Q

What is an event loop in asyncio?

A

The mechanism that schedules and runs coroutines. Typically started with asyncio.run(main()).

19
Q

Which functions schedule or group coroutines?

A

asyncio.create_task() schedules a coroutine; asyncio.gather() runs multiple coroutines concurrently and waits for all.

20
Q

How is async different from multiprocessing?

A

Async runs in one process and one CPU core, focusing on non-blocking I/O; multiprocessing runs code truly in parallel on multiple cores.

21
Q

Give an example of creating and running a single async task.

A

async def my_task():
await asyncio.sleep(2)
print(‘Task completed’)

async def main():
task = asyncio.create_task(my_task())
print(‘Main continues…’)
await asyncio.sleep(3)

asyncio.run(main())