[Tutor] Decorators, I am confused.

David Driver count0.djd at gmail.com
Fri Jul 15 02:46:22 CEST 2005


What I would like to do is something like this:

class testObj(object):
    _rules = list()
    def evalRules(self):
        for r in _rules:
            r()
    def rule(func):
        if not func in func.__class__._rules:
            func.__class__._rules.append(func)
            print testis._rules
        return func
    @rule
    def arule(self):
        print a

Of course it doesn't work. Is there a way to make it work? What I am
trying to do is make a collection of validation rules that can be
iterated and capture raised exceptions to be returned at the end of
eval rules.

I am trying to replace somethng that looks like this:

class testObj(object):
    def _init__(self):
        self._rules = list((self.arule,
                            self.anotherrule))
    def evalRules(self):
        for r in self._rules:
            r()

It would be too easy to add a rule twice or forget to add the rule to
the list in a subclass. I thought that using a decorator would make it
obvious.



Thanks!
-- 

***********************************
See there, that wasn't so bad.
***********************************


More information about the Tutor mailing list