python decorator to add function to list in current scope
- by AlexH
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"]