On Tue, Apr 10, 2012 at 11:36 PM, Kiuhnm <kiuhnm03.4t.yahoo...@mail.python.org> wrote: > On 4/10/2012 14:29, Ulrich Eckhardt wrote: >> >> Am 09.04.2012 20:57, schrieb Kiuhnm: >>> >>> Do you have some real or realistic (but easy and self-contained) >>> examples when you had to define a (multi-statement) function and pass it >>> to another function? >> >> >> Take a look at decorators, they not only take non-trivial functions but >> also return them. That said, I wonder what your intention behind this >> question is... >> > > That won't do. A good example is when you pass a function to re.sub, for > instance.
The most common case of such a thing is a structure walking utility. For instance, a linked-list walker could be written as: def walk(tree,func): node=tree.head while node: func(node.data) node=tree.sibling This could equally reasonably be written with yield, though I'm not sure that it's possible to write a recursive generator as cleanly (eg to walk a binary tree). Perhaps the new "yield from" syntax would be good here, but I've never used it. In any case, it's a classic use of passing a function-like as a parameter, even if there's another way to do it. ChrisA -- http://mail.python.org/mailman/listinfo/python-list