KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I still haven't got my head around decorators in Python. I've already started using a lot of closures to do things like customize functions and classes in my coding. Eg. 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,[])])]) def pp(n) : print "%s," % n.val printTree = makeRunner(pp) printTree(tree) As far as I can see, decorators are just a different syntax for doing something similar. Instead of def pp(n) : print "%s," % n.val printTree = makeRunner(pp) I would write : @makeRunner def printTree(n) : print "%s," % n.val Is this all there is to decorators? Or is there a fundamental difference that I've missed?
Tags (comma-separated)
Save Edits
Cancel