[Persistence-sig] Naive questions about getting and setting
Donnal Walter
donnalcwalter@yahoo.com
Wed, 24 Jul 2002 01:12:35 -0700 (PDT)
1. Would naive (and rather application specific) questions such as
these be better posed to comp.lang.python? If so, I would happily
comply in the future.
2. In regard to the persistence API and especially in regard to
observation, would someone please point out the pitfalls of using
so-called "setter" and "getter" methods in attribute classes
themselves, as opposed to __setattribute__ and __getattribute__
methods in the container classes?
I am in no way proposing this as a general solution, but if in a
given situation one wanted to set up a scheme similar to that coded
below, what would be the major liabilities? Is it simply a matter
of lack of transparency? Or is there also a serious problem with
decreased efficiency?
=======
class Cell(object):
def __init__(self, *args):
"""Arguments, if any, must be references to other cells."""
self.__value = None # the scalar value of the Cell
self.__observers = [] # list of observers
if len(args) > 0: # if this Cell is dependent
self.ref = args # save the list of references
for i in self.ref: # for every external ref
i.AddObserver(self) # register as an observer
self.Update() # set initial from refs
else: # if this Cell is independent
self.Reset(content) # simply reset its value
def AddObserver(self, observer):
if observer not in self.__observers:
self.__observers.append(observer)
def _setValue(self, value):
if value != self.__value:
self.__value = value
for o in self.__observers:
o.Update()
def _getValue(self):
return self.__value
def Set(self, input):
try:
# make sure input can be converted
self._setValue(self.Encode(input))
except ValueError:
# if incompatible input value, reinitialize
self.Reset()
def Get(self):
return self.Decode(self._getValue())
def Encode(value):
""" override to change Cell type"""
return value
def Decode(value):
""" override to change Cell type"""
return value
def Update(self):
""" Override in observer Cells.
(Observers have access to the self.ref list.)
"""
pass
def Reset(self):
""" may be overridden to change default value"""
self._setValue('')
=======
=====
Donnal Walter
Arkansas Children's Hospital
__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com