[Chicago] Creating property() members programmatically

Tim Gebhardt tim at gebhardtcomputing.com
Mon Feb 9 16:13:45 CET 2009

On Sat, Feb 7, 2009 at 10:57 AM, Brantley Harris <deadwisdom at gmail.com>wrote:

> So it's easy enough to declare properties on classes, but rather
> difficult to do so on objects themselves (I'm not even sure it's
> possible, well not good form anyhow.)
> So you have a few options.  One is to subclass your Encoder with an
> intermediate class, and then add properties, dynamically to it:
>  class LameEncoder(Encoder):
>    @classmethod
>    def add_property(cls, attr, default):
>      def setter(self, v):
>        setattr(self, '_%s' % attr, v)
>      def getter(self):
>        return getattr(self, '_%s' % attr, default)
>      setatter(cls, attr, property(gettter, setter))
>  for attr, default in PARAMETER_LIST:
>    LameEncoder.add_property(atter, default)

I went with the first method you suggested and it worked perfectly.  Thanks!

-Tim Gebhardt
tim at gebhardtcomputing.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20090209/f70b7b1d/attachment.htm>

More information about the Chicago mailing list