Created
October 3, 2011 01:50
-
-
Save utsengar/1258272 to your computer and use it in GitHub Desktop.
Decorators as syntatic sugar to closures in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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