2.2 properties and subclasses

Miles Egan miles at rddac.com
Tue May 20 12:11:48 EDT 2003


In article <just-AA3639.09530820052003 at news1.news.xs4all.nl>, Just wrote:
> In article <slrnbcj414.vot.miles at car.pixar.com>,
>  Miles Egan <miles at rddac.com> wrote:
> 
>> Repeating the property declaration for every property becomes painful
>> when you have a dozen classes with 10 properties each.  There's
>> already a fair amount of boilerplate you have to write to set the
>> properties up in the base class.
> 
> You could write a metaclass that automatically creates properties (say 
> using a naming convention):
> 
> class AutoProperty(type):
>     def __new__(cls, name, bases, methoddict):
>         for key, value in methoddict.items():
>             if key.startswith("get_"):
>                 methoddict[key[4:]] = property(value)
>         return type.__new__(cls, name, bases, methoddict)

That's an interesting idea.  I haven't messed around with metaclasses
at all yet.  Thanks for the suggestion.

-- 
miles egan
miles at rddac.com

reverse the domain name to find me

OpenPGP/GPG Key 0x01F53D51 @ wwwkeys.us.pgp.net




More information about the Python-list mailing list