Adding properties to an instance

dg.google.groups at thesamovar.net dg.google.groups at thesamovar.net
Wed Feb 6 23:53:11 CET 2008


On Feb 6, 10:54 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> I'd suggest a small improvement: _A as a class name isn't very nice.
> Replace the inner class statement with:
> _A = type(self.__class__.__name__ + '_autoprops', (self.__class__,), {})

Ah yes, that's much nicer.

> A problem with this approach is that instances aren't pickleable (perhaps
> that could be solved using __reduce__)

I think because the properties can be computed from knowing the rest
of the data of the object, it would be safe when pickling to just
pickle a copy of the object with the __class__ changed back to A (and
then when you load it again, you can just generate the properties
anew). I haven't really thought much about pickling but it would
certainly be a nice feature to have.

--
Dan



More information about the Python-list mailing list