python decorator to add function to list in current scope
Posted
by AlexH
on Stack Overflow
See other posts from Stack Overflow
or by AlexH
Published on 2010-04-26T14:29:19Z
Indexed on
2010/04/26
14:33 UTC
Read the original article
Hit count: 263
Goal: Make a decorator which can modify the scope that it is used in.
If it worked:
class Blah(): # or perhaps class Blah(ParentClassWhichMakesThisPossible) def one(self): pass @decorated def two(self): pass >>> Blah.decorated ["two"]
Why? I essentially want to write classes which can maintain specific dictionaries of methods, so that I can retrieve lists of available methods of different types on a per class basis. errr.....
I want to do this:
class RuleClass(ParentClass): @rule def blah(self): pass @rule def kapow(self): pass def shazam(self): class OtherRuleClass(ParentClass): @rule def foo(self): pass def bar(self): pass >>> RuleClass.rules.keys() ["blah", "kapow"] >>> OtherRuleClass.rules.keys() ["foo"]
© Stack Overflow or respective owner