Properties fun with 2.2

Mike C. Fletcher mcfletch at rogers.com
Mon Dec 24 03:15:45 EST 2001


For a long time I've wanted a way to define properties for Python 
classes that have all the automated checking you'd expect from 
properties (in order to automatically create GUIs):

	only defined properties can be assigned to
	assignment/retreival is with standard syntax (x.y)
	only values of the appropriate type can be assigned, allow for automatic 
bounds checking and the like
	values are (intelligently) coerced to proper types if possible
	data is stored in the class using standard mechanisms (__dict__) (make it 
easy for things such as pickling, introspection, etceteras to work 
without special support)
	allow for introspection of the property definition to allow for declaring 
appropriate editing controls, events, etceteas (in GUI environments)
	allow completely different functionality for each property


Well, just did some playing with the 2.2 properties class/type, and it 
turns out it's easily sub-classable to create something to support the 
list above.


class SimpleProperty( property ):
	def __init__( self, name, documentation = "" ):
		property.__init__( self, self.getValue, self.setValue, self.delValue, 
documentation )
		self.name = name
	def getValue( self, client ):
		return client.__dict__[ self.name ]
	def setValue( self, client, value ):
		client.__dict__[ self.name ] = value
	def delValue( self, client ):
		del client.__dict__[ self.name ]

class X( object ):
	first = SimpleProperty( 'first', 'testing documentation' )


So, to define a new property type (for instance, a property type whose 
values must always be subclasses of a given type/class):


class CheckedProperty( SimpleProperty ):
	def __init__( self, name, documentation = "", dataType = None ):
		self.dataType = dataType
		SimpleProperty.__init__( self, name, documentation )
	def setValue( self, client, value ):
		if not isinstance( value, self.dataType ):
			raise TypeError( '''The value %s is not of the required type 
%s'''%(repr(value), self.dataType))
		return SimpleProperty.setValue( self, client, value )

class Y( object ):
	first = CheckedProperty( 'first', 'testing documentation', int )


The "only defined properties" thing can be done with a __slots__ 
definition in the object, but then there's no __dict__ in which to store 
the data (there must be some way to say "store in the slot", I just 
don't see it right now).

Real-world examplex of property classes will likely wind up being fairly 
complex (with shared coercian mechanisms/codecs, application-specific 
coding, etcetas), but a core set might be workable, possibly something 
that can support extensions for common requirements (e.g. bounds and 
type checking, or coercian plugin frameworks (into which you'd plug your 
application-specific checkers/coercers)).

Anyway, just thought others might be interested in the new world which 
has opened at our feet :o) ;) .  Thanks dev peoples!  Much appreciated!

Enjoy yourselves,
Mike
_______________________________________
   Mike C. Fletcher
   http://members.rogers.com/mcfletch/






More information about the Python-list mailing list