Adding properties to an instance

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Feb 6 22:54:30 CET 2008


En Wed, 06 Feb 2008 18:06:48 -0200, <dg.google.groups at thesamovar.net>  
escribió:

> So I understand that properties belong to a class not an instance, but
> nonetheless I want to add properties to an instance. I have a class
> which when an instance is created runs some fairly complicated code
> and produces a set of names which I'd like to be able to access via
> properties.

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__,), {})

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

-- 
Gabriel Genellina




More information about the Python-list mailing list