Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function pointers in python

I would like to do something like the following:

def add(a, b):
    #some code

def subtract(a, b):
    #some code

operations = [add, subtract]
operations[0]( 5,3)
operations[1](5,3)

In python, is it possible to assign something like a function pointer?

like image 521
t3rse Avatar asked Apr 12 '26 23:04

t3rse


1 Answers

Did you try it? What you wrote works exactly as written. Functions are first-class objects in Python.

like image 144
Joe Ganley Avatar answered Apr 15 '26 12:04

Joe Ganley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!