Properties fun with 2.2
Mike C. Fletcher
mcfletch at rogers.com
Tue Dec 25 13:23:14 EST 2001
Hmm, don't really understand what the contract of the result is supposed
to be:
A sequence object, where a "controller sequence" declares property
objects. These property objects control the data type, allow for bounds
checking, and do the actual work of storing/retreiving data.
or
A sequence object, where the sequence object has "numerically indexed
attributes". That is, a "sparse sequence of attributes" where each
attribute is controlled by a particular Property object (that is, data
type, bounds checking, storage/retrieval are all handled by the
delegated object).
or
Something else?
class ControlledSequence:
# there's no 2.2-specific content here...
def __getitem__( self, index ):
return self.__properties[index].__get__( self, index )
# same for set and del, put in some slice logic with iteration
class ControlledSequenceProperty:
def __get__( self, client, index ):
#this isn't the same API as standard property type
#see the property example for what you'd do here,
#basically just get/set/del the value from the client
Where, depending on the contract, __properties is a list or a dictionary.
[2.2 content here]
One thing I really don't get in the property design is why the property
__get__ __set__ __del__ methods don't normally have an API like that
above (i.e. pointer to property object, pointer to client object, and
pointer to the key/index/attributename used to call the property). With
that, you could re-use the same property object if the property
attributes were identical.
Enjoy,
Mike
Robin Becker wrote:
...
> very nice. I wondered if this stuff would be easy and it seems it can
> be. One of the things I need is a collection of properties ie a property
>
> P
>
> with its own attributes (probably properties themselves) which can be
> accessed as
>
> P[0]
>
> ie a thing similar to P, but allowed individual values where these
> differ from the aggregate. I wonder whether this is more easily done in
> 2.2. It's quite hard in 2.1.
...
More information about the Python-list
mailing list