It is natural to see functions within expressions,
even as arguments to another function. The
expression abs(5-len("counterparty"))
shows uses two functions, abs and len. To understand
how Python evaluates such expressions, the example
below traces through some functions by putting
print statements into the function (even though the
print statements serve no useful purpose, they
have the benefit of forcing output so that we can
see the order of evaluation).
def double(x): |
print "by", |
return 2*x |
def increment(y): |
print "one", |
return 1+y |
def incdouble(z): |
r = increment(double(increment(z))) |
print "results are" |
return r |
v = incdouble(5) |
print "calculated to be =", v |