[Tutor] automatic setting of class property when another one changes
Steven D'Aprano
steve at pearwood.info
Thu Sep 15 09:35:11 EDT 2016
On Thu, Sep 15, 2016 at 12:30:34PM +0200, ingo wrote:
> Rather stuck with this one, I'd like to automatically (re)set the
> propery "relaystate" when one of the others (Tm, Tset, Th) is set,
> regardless of wether their value has changed.
The obvious way is to make Tm, etc properties, and have their setter
method reset the relaystate property. Since they relay state cannot be
calculated during __init__ until all of the properties have been set,
simply bypass the properties in the __init__. See below.
> My code so far:
Thanks for showing your code, but you shouldn't drown us in masses of
irrelevant code that has nothing to do with your problem. We don't care
about the logging code you show. Take it out. In fact, the best way to
demo code is to cut it down to the barest minimum demonstrating the
problem:
class Thermostat(object):
def __init__(self, Tm):
self._Tm = Tm
self.reset_relay()
@property
def Tm(self):
return self._Tm
@Tm.setter
def Tm(self, value):
self.reset_relay()
self._Tm = value
def reset_relay():
self.relaystate = None
About a dozen lines, versus seventy for your code.
See also this for more information:
http://sscce.org/
--
Steve
More information about the Tutor
mailing list