Embedding questions

Carel Fellinger cfelling at iae.nl
Fri Dec 10 20:08:38 EST 1999


Hi Olaf,

reading the other postings in this tread and learning more of what you
seek to achieve, I got second thoughts: maybe my proposal might not be
that ugly after all. Let us expand on it:

>>> class Currency:
>>>     def __init__(self, value=None, **others):
>>>         self.__dict__.update(others)
>>>         self.read, self.written = 0, 0
>>>         if value != None:
>>>             self(value)
>>>
>>>     def __call__(self, value=None):
>>>         if value == None:
>>>             self.read = self.read + 1
>>>             return self.value
>>>         else:
>>>             self.value = value
>>>             self.written = self.written + 1
>>>
>>> c = C(3, state_var='Pipo de Clown')
>>> c(5)
>>> print c(), c.read, c.written
5 1 2

This class seems to offer what you need: it keeps track of read and write
access to the value attribute, its notation is concise and familiar. Read
and write access look like applying a SUB, the write one with side effects,
probably not that unfamiliar to BASIC programmers:)

To me the use of functions to get to the value is quite acceptable
considering that c has some state info that you normally discard.
OTOH assignment to c that leaves the extra state info unchanged is
much harder to appreciate for me (been brainwashed to long:)
-- 
groetjes, carel



More information about the Python-list mailing list