A better way of making subsclassing of built-in types stick for attributes?

Maric Michaud maric at aristote.info
Wed May 17 10:11:53 EDT 2006


Le Mercredi 17 Mai 2006 06:17, telesphore4 at gmail.com a écrit :
> I want the fact that the fields are
> not strings to be invisible to the client programmers.
You should use properties then.

In [12]: class mystr(str) : pass
   ....:

In [13]: class myrow(object) :
   ....:         getX = lambda s : s._x
   ....:         setX = lambda s, v : setattr(s, '_x', mystr(v))
   ....:         X = property(getX, setX)
   ....:
   ....:

In [14]: r=myrow()

In [15]: r.X = 'toto'

In [16]: r.X
Out[16]: 'toto'

In [17]: type(r.X)
Out[17]: <class '__main__.mystr'>


-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097



More information about the Python-list mailing list