design question: no new attributes

Alan Isaac aisaac at american.edu
Thu Mar 1 15:39:17 CET 2007


"greg" <greg at cosc.canterbury.ac.nz> wrote in message
news:54njqjF21q8jlU1 at mid.individual.net...
> There's a problem with that when you want to subclass:

Agreed.  The following addresses that and, I think, some
of the other objections that have been raised.
Alan

class Lockable:
    a = 0
    def __init__(self, lock=False):
        self.b = 1
        self.lock = lock
    def __setattr__(self, attr, val):
        if not hasattr(self,attr) and hasattr(self,'lock') and self.lock:
            raise ValueError("This object accepts no new attributes.")
        self.__dict__[attr] = val





More information about the Python-list mailing list