Funktionen

Funktionen sind Objekte wie alles andere auch

def add(a, b): return a+b
def mul(a, b): return a*b
def cpr(op, l):
  s=l[0]
  for n in l[1:]: s=op(s,n)
  return s
def makefunction(msg):
  def fn(): print msg
  return fn
>>> cpr(add, [1, 2, 3, 4, 5])
15
>>> cpr(mul, [1, 2, 3, 4, 5])
120
>>> sayhello=makefunction("Hello")
>>> sayworld=makefunction("World")
>>> sayhello(); sayworld()
Hallo
Welt

Lambda-Formen

add=lambda a, b: a+b
mul=lambda a, b: a*b
cpr(lambda a, b: a*b, [1, 2, 3, 4, 5])