0% found this document useful (0 votes)
13 views5 pages

48 Callback Functions

This document provides an overview of callback functions in Python, explaining their purpose and common use cases such as UI actions, async programming, and threading. It includes step-by-step examples demonstrating how to implement callback functions and highlights the benefits of using them, such as decoupling logic and reusability. Additionally, it contrasts callback functions with higher-order functions and offers a brief author bio for Gowtham SB, a data engineering expert.

Uploaded by

joanantoranjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

48 Callback Functions

This document provides an overview of callback functions in Python, explaining their purpose and common use cases such as UI actions, async programming, and threading. It includes step-by-step examples demonstrating how to implement callback functions and highlights the benefits of using them, such as decoupling logic and reusability. Additionally, it contrasts callback functions with higher-order functions and offers a brief author bio for Gowtham SB, a data engineering expert.

Uploaded by

joanantoranjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Gowtham SB

[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil

🎬 Python Guide: Callback Functions

✅ What is a Callback Function?


A callback function is a function that you pass as an argument to another
function,
so that it can be called (executed) later, usually after some action is completed.

🎯 Common in:

● UI actions (like button clicks)

● Async programming

● Threading

● Event handling

🔧 Step-by-Step Example 1: Simple Callback with


print_status()
def process_data(data, callback):
print("Processing:", data)
# Once processing is done, call the callback
callback()

def print_status():
print("✅ Data processed successfully!")

process_data("user-info", print_status)

🎯 Explanation:

● process_data() accepts a function callback as a parameter


Gowtham SB
[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil

● After its job is done, it calls callback()

● print_status() is passed, not executed (print_status, not print_status())

⚙️Example 2: Callback in UI Simulation (Button Click)


def on_button_click(callback):
print(" Button clicked")
callback()

def show_message():
print("👋 Hello Gowtham, welcome!")

on_button_click(show_message)

✅ Whenever the button is “clicked,” show_message() is called through the callback.

⚙️Example 3: Callback in Threading


import threading
import time

def long_task(callback):
print("🚀 Task started...")
[Link](2)
print("✅ Task finished")
callback()

def after_done():
print("📢 Callback: Now update the UI!")

# Running long task in a separate thread


thread = [Link](target=long_task, args=(after_done,))
[Link]()
Gowtham SB
[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil

🧠 After the background task is done, it calls after_done() automatically


🎯 This is how many UI frameworks or APIs update progress bars or show popups after
completion.

📦 Real-World Use Cases


Use Case What the Callback Does

Button click Run a function when user clicks

File upload Call on_success() after upload

Background thread Call on_complete() after


processing

API request Call on_response(data) after


fetch

🧠 Why Use Callback Functions?


Benefit Explanation

Decouples logic Main function doesn’t need to know what happens


next

Reusable Can plug different callbacks for different actions

Required in Wait → then call callback


async/threading

Event-driven design Perfect for UI & real-time systems

⚠️Common Mistake to Avoid


# WRONG: You’re calling the function immediately
process_data("info", print_status()) # ❌ this executes the function, not passes it

# RIGHT
process_data("info", print_status) # ✅ this passes the function as callback
Gowtham SB
[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil

🔁 Callback vs HOF
Feature Callback Function Higher-Order Function

Purpose Execute after Compose logic dynamically


something

Who controls call Main function Caller function

Examples on_click, on_done, map(), filter(),


etc decorators

✅ TL;DR in 1 Line:
A callback is a function you pass to another function, so it can be called back
later — often after an event or async task.

About the Author


Gowtham SB
[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil

Gowtham SB is a Data Engineering expert, educator, and content creator with a


passion for big data technologies, as well as cloud and Gen AI . With years of
experience in the field, he has worked extensively with cloud platforms, distributed
systems, and data pipelines, helping professionals and aspiring engineers master the
art of data engineering.

Beyond his technical expertise, Gowtham is a renowned mentor and speaker, sharing
his insights through engaging content on YouTube and LinkedIn. He has built one of
the largest Tamil Data Engineering communities, guiding thousands of learners to
excel in their careers.

Through his deep industry knowledge and hands-on approach, Gowtham continues to
bridge the gap between learning and real-world implementation, empowering
individuals to build scalable, high-performance data solutions.

𝐒𝐨𝐜𝐢𝐚𝐥𝐬

𝐘𝐨𝐮𝐓𝐮𝐛𝐞 - [Link]

𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - [Link]

𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - [Link]

𝐂𝐨𝐧𝐧𝐞𝐜𝐭 𝐟𝐨𝐫 𝟏:𝟏 - [Link]

𝐋𝐢𝐧𝐤𝐞𝐝𝐈𝐧 - [Link]

𝐖𝐞𝐛𝐬𝐢𝐭𝐞 - [Link]

𝐆𝐢𝐭𝐇𝐮𝐛 - [Link]

𝐖𝐡𝐚𝐭𝐬 𝐀𝐩𝐩 - [Link]

𝐄𝐦𝐚𝐢𝐥 - [Link]@[Link]

𝐀𝐥𝐥 𝐌𝐲 𝐒𝐨𝐜𝐢𝐚𝐥𝐬 - [Link]

You might also like