Szabolcs said:
Function c() is not meant to be called from outside function a().
That's what a nested function is for: localizing it's usage and
prevent cluttering the global namespace
There's actually more than this about Python's nested functions: they
can be returned from the outer function and then carry the environnement
in which they where created:
def trace(func):
fname = func.__name__
def traced(*args, **kw):
print "calling func %s with *%s, **%s" % (fname, str(args), kw)
try:
result = func(*args, **kw)
except Exception, e:
print "%s raised %s" % (fname, e)
raise
else:
print "%s returned %s" % (fname, str(result))
return result
return traced
def test(arg1, arg2='parrot'):
print "in test, arg1 is %s" % arg1
return arg2 * 3
test = trace(test)
test(42)