본문 바로가기
Python(Eng. ver)

14. Callbacks and Lambdas in Python

by 곽정우 2024. 3. 18.

1. Callback Function

A callback function is a function that is passed as an argument to another function and is executed when a certain event or condition occurs. Callback functions are commonly used in asynchronous operations or when you want to perform a specific action when a certain event occurs.

 

 

2. Lambda Function

A lambda function is a special syntax in Python for creating simple anonymous functions. "Anonymous function" means that the function does not have a unique name assigned to it. Lambda functions are mainly used to create short and concise functions that can be expressed in a single line, unlike regular functions defined using def.

arguments: The arguments passed to the lambda function.

expression: The expression to be evaluated. The value of this expression is returned when the lambda function is called.

3. Representative Functions Where Lambda is Useful

3-1.  Filter Function

Filter() is a built-in Python function that returns an iterator consisting only of items that satisfy the condition of the given function. This function is commonly used to filter out items that do not meet certain criteria from a list or other sequential data type.

function: The function applied to each item. This function takes one argument and returns True or False.

iterable: The iterable object to be filtered (e.g., list, tuple, string).

 

3-2. Map Function

Map() is a built-in Python function that creates an iterator that applies the given function to each item of the iterable and returns the result. This function is commonly used to apply a function to each item in a list or other sequential data type.

function: The function to be applied to each item.

iterable: The iterable object to be processed by function (e.g., list, tuple, string). map() can handle multiple iterables simultaneously.

'Python(Eng. ver)' 카테고리의 다른 글

16. Inheritance in Python  (0) 2024.03.19
15. Object-oriented programming and Class  (0) 2024.03.18
13. Scope of Variable in Python  (0) 2024.03.18
12. User-Defined Function  (0) 2024.03.15
11. Collections and Loops  (0) 2024.03.15