Main Content
What are callbacks? Why use them? An example in Python Archive - Originally posted on "The Horse's Mouth" - 2016-02-11 08:38:42 - Graham Ellis
A callback is a function that's called from within another, having initially been "registered" for use at an outer level earlier on. Yeah, right - what does that mean or do?
An example ... if I'm setting up a GUI (Graphic User Interface), as I define my various sliders and buttons, I want to say "when this is pressed, do xxxx", but I don't actually want to do "xxxx" until the button is pressed - probably some considerabel time after the initial definition.
An example from this week's Python Course - see [here] .
I've defined a couple of methods for running later such as:
def inflate(value):
return value * 1.7
I've passed those methods into an object
john = thing(12,inflate)
and used a method I've chosen to call "perform" to run the method later:
print(john.perform())
To complete the example, here's the class:
class thing:
def __init__(this,amount,action):
this.amount = amount
this.action = action
def perform(this):
return this.action(this.amount)
Some other articles
Q101 - Programming Principles This article Remember to ask the question before you listen for the answer Hungarian, Camel, Snake and Kebab - variable naming conventions Learning to program - what are algorithms and design patterns? Writing the perfect program in Tcl? Rooms available tonight - how to code an algorithm from first principles We not only teach PHP and Python - we teach good PHP and Python Practice! Test Driven Development in Python - Customer Comes First Seamless, integrated IT - we have a long way to go! Web and console - same principle, same code - Ruby example Lesson 1 in programing - write clean, reuseable and maintainable tidy code Storing your intermediate data - what format should you you choose? From Structured to Object Oriented Programming. Object oriented or structured - a comparison in Python. Also writing clean regular expressions Some terms used in programming (Biased towards Python) Dark mornings, dog update, and Python and Lua courses before Christmas What order are operations performed in, in a Perl expression? Stepping stones - early coding, and writing re-usable code quickly Coding efficiency - do not repeat yourself! An introduction to file handling in programs - buffering, standard in and out, and file handles Looking up a value by key - associative arrays / Hashes / Dictionaries Program for reliability and efficiency - do not duplicate, but rather share and re-use Easy - but for whom? Improving your function calls (APIs) - General and PHP And and Or illustrated by locks Do not copy and paste code - there are much better ways The music of the stock market Variable names like i and j - why? Planning! Learning to write high quality code in Lua Where do I start when writing a program? Pre and post increment - the ++ operator I have not programmed before, and need to learn Y105 - Functions, Modules and Packages From and Import in Python - where is the module loaded from? Embedding more complex code into a named block Nesting decorators Recursion in Python - the classic example This article What is the difference between a function and a method? Reading command line parameters in Python A good example of recursion - a real use in Python Python - even named code blocks are objects Multiple yields and no loops in a Python generator? Python functions - an introduction to how they work Python varables - checking existance, and call by name or by value? Exception, Lambda, Generator, Slice, Dict - examples in one Python program vargs in Python - how to call a method with unknown number of parameters Optional positional and named parameters in Python Default local - a good choice by the author of Python Static variables in Python? Python timing - when to use a list, and when to use a generator Functions are first class variables in Lua and Python Finding all the unique lines in a file, using Python or Perl Python Packages - groupings of modules. An introduction Static variables in functions - and better ways using objects Passing optional and named parameters to python methods Catching the fishes first? Passing parameters to Python functions - the options you have Returning multiple values from a function call in various languages - a comparison Using an exception to initialise a static variable in a Python function / method Python - some common questions answered in code examples Passing a variable number of parameters in to a function / method Program for reliability and efficiency - do not duplicate, but rather share and re-use Optional and named parameters to Python functions/methods Python - access to variables in the outer scope Global and Enable - two misused words! Good example of recursion in Python - analyse an RSS feed Sample code with errors in it on our web site Optional parameters to Python functions Multiple returns from a function in Python Conversion of OSI grid references to Eastings and Northings Dynamic code - Python Optional and named parameters in Python What to do with a huge crop of apples Anonymous functions (lambdas) and map in Python Sharing variables with functions, but keeping them local too - Python Global - Tcl, PHP, Python Python Script - easy examples of lots of basics Returning multiple values from a function (Perl, PHP, Python) A better alternative to cutting and pasting code Function / method parameters with * and ** in Python It's the 1st, not the 1nd 1rd or 1th. Sludge off the mountain, and Python and PHP Python - A list of methods Recursion in Python Python - function v method Dynamic functions and names - Python Do not duplicate your code Cottage industry or production line data handling methods Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus. Python - block insets help with documentation Python's Generator functions Difference between import and from in Python What is a callback? Code and code maintainance efficiency Call by name v call by value Lambdas in Python Python generator functions, lambdas, and iterators Distance Learning Variable Scope