metaclass & __slots__

holger krekel pyth at devel.trillke.net
Fri Jul 12 16:16:00 EDT 2002


Steve Tregidgo wrote:
> phoebe_1 at att.net (Holden Caulfield) wrote in message news:<c2595393.0207031317.7d9bb013 at posting.google.com>...
> > class MX(type):
> >     def __init__(cls,name,bases,dict):
> >         super(MX,cls).__init__(name,bases,dict)
> >         props = {}
> >         slots = getattr(cls,'__slots__', [])
> >         print slots
> >         for v in dict.keys():
> >             vs = v.startswith
> >             if vs("_get_") or vs("_set_"):
> >                 props[v[5:]] = 1
> >         for v in props.keys():
> >             fget = getattr(cls,"_get_%s" % v, None)
> >             fset = getattr(cls,"_set_%s" % v, None)
> >             setattr(cls,v,property(fget,fset))
> >         slots.append("_%s__%s" % (name,v))
> >         setattr(cls,'__slots__',slots)
> 
> The indentation of the 'slots.append' line is such that the statement
> falls outside of the preceding 'for' loop.  In the example that you
> gave, this would not have shown up whilst running the code.

Wasn't python supposed to be much safer against off-by-one errors?

:-) holger





More information about the Python-list mailing list