[Tutor] Decorators, I am confused.

Alan G alan.gauld at freenet.co.uk
Fri Jul 15 08:11:50 CEST 2005


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


Can you explain in plain English what testObj does?
I don;t understand the comncept you are trying to represent
in this testObj. It seems to be executing a list of its own
methods? And its methods when executed add them selves to
the list?!

> 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'd expect the rules to be external to the class. Either
functions or perhaps methods of another object that gets passed
to testObj.

>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):

Yes that's how I'd expect to find it although perhaps with an 
addRule() method
for extending the rule set. But it really depends on what the 
responsibilities
of testObj are. Is it an object for testing other objects or simply an 
object
for testing the concept of running several rules?

> 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.

Not sure I understand this bit. If you ony want a rule once you could 
use
a Set instead of a list. And how the above prevents subclasses from 
forgetting
a rule isn't clear to me.

I think I'm missing the grand plan here.

Alan G.




More information about the Tutor mailing list