properties setting each other

Maric Michaud maric at aristote.info
Wed Sep 3 18:11:25 CEST 2008


Le Wednesday 03 September 2008 17:40:43 mk, vous avez écrit :
> > Note that if one property can really be computed from another, this kind
> > of thing could be considered as bad design (except if the computation is
> > heavy).
>
> Hmm, why? Is the line of thinking smth like: because the variables
> should be kept to minimum and they should be calculated at the moment
> they are needed?

Because you have to make extra effort to keep the logical relation between 
value and square. self._square is not really needed, and what is not needed 
is just extra hassle.

Doesn't it clear that your code is more hard to maintain than the 
alternative :

class Squared(object):

        def __init__(self, val):
                self._val=val
                
        def fgetvalue(self):
                return self._val
        def fsetvalue(self, val):
                self._val=val
        value = property(fgetvalue, fsetvalue)

        def fgetsquare(self):
                return self.value ** 2
        def fsetsquare(self,s):
                self.value = math.sqrt(s)
        square = property(fgetsquare, fsetsquare)



-- 
_____________

Maric Michaud



More information about the Python-list mailing list