\(\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}\)
Functions are objects¶
In Python, functions are objects, like any other object.
If I make a string in Python:
>>> name = 'Matthew'
then I have a Python object of type str
:
>>> type(name)
<class 'str'>
Let’s say I defined a function add
:
>>> def add(a, b):
... return a + b
Now I have another Python object, of type function
:
>>> type(add)
<class 'function'>
With my string, I can refer to the same string object, with a different variable name:
>>> prisoner = name
>>> prisoner
'Matthew'
It’s the same for functions, because functions are objects too:
>>> my_add = add
>>> type(my_add)
<class 'function'>
Functions are objects you can “call” by appending parentheses enclosing arguments you want to pass:
>>> add(1, 2)
3
>>> my_add(1, 2)
3
As for any other object in Python, you can pass function objects to other functions:
>>> def run_a_func(func, arg1, arg2):
... result = func(arg1, arg2)
... print('Result was', result)
>>> run_a_func(add, 1, 2)
Result was 3
>>> run_a_func(my_add, 1, 2)
Result was 3
>>> def sub(a, b):
... return a - b
>>> run_a_func(sub, 1, 2)
Result was -1