Function Objects

What’s a Function?

First: what’s a variable?

  • A name that refers to something (here: an integer object)

  • Created at first assignment

i = 1

Functions are no different …

  • The function’s name refers to a function object

  • … it’s just that object creation is done differently

def square(number):
    """
    return square
    of the argument
    """
    return number**2

Function Objects?

square is a name that happens to refer to a function object …

Object and its attributes
>>> square
<function square at 0x7fca2c785b70>
>>> square.__doc__
'\n    return square\n\tof the argument\n\t'
The () Operator
>>> square(3)
9

Function Objects! (1)

Dynamic languages require care
>>> square = 1
>>> square(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
Assign one variable to another
op = square
op(3)

Function Objects! (2)

Function as function argument
def forall(op, list):
    result = []
    for elem in list:
        result.append(op(elem))
    return result

print(forall(square, [1, 2, 3]))
print(forall(len, ["Joerg", "Faschingbauer"]))
This will output …
[1, 4, 9]
[5, 13]

Batteries included: Python built-in function map