def foo(thing, block): thing = "cruel " + thing block(thing) # like `yield thing' or 'block.call' in ruby def bar(thing): return "hello " + thing foo('world', bar) # or: foo('world', lambda x: "hello " + x)
def foo(thing) yield thing end foo('world') { |thing| "hello " + thing }
Ruby:
def foo(bar) if block_given? yield bar end end foo 5 { |x| puts x } # prints 5
def foo(bar,fcn): fcn(bar) foo(5, lambda x: sys.stdout.write("%d\n" % x))
edit Oops, had this page open for a while, didn't realize someone had replied in the interim, making my response somewhat redundant