Skip to content

Instantly share code, notes, and snippets.

@utsengar
Created October 3, 2011 01:50
Show Gist options
  • Save utsengar/1258272 to your computer and use it in GitHub Desktop.
Save utsengar/1258272 to your computer and use it in GitHub Desktop.
Decorators as syntatic sugar to closures in Python
class Node :
def __init__(self,val,children) :
self.val = val
self.children = children
def makeRunner(f) :
def run(node) :
f(node)
for x in node.children :
run(x)
return run
tree=Node(1,[Node(2,[]),Node(3,[Node(4,[]),Node(5,[])])])
#Using decorators
@makeRunner
def printTree(node) : print "%s," % node.val
#Simple closures
#def pp(node) : print "%s," % node.val
#printTree = makeRunner(pp)
printTree(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment