design question: no new attributes

Alan Isaac aisaac at american.edu
Tue Feb 27 15:59:03 EST 2007


"Steven D'Aprano" <steve at REMOVEME.cybersource.com.au> wrote in message
news:pan.2007.02.27.07.24.57.769316 at REMOVEME.cybersource.com.au...
class Difficult(object):
    def __setattr__(self, name, value):
        if self.__dict__.has_key(name):
            print "'%s' exists as an instance attribute" % name
            self.__dict__[name] = value
        elif self.__class__.__dict__.has_key(name):
            print "'%s' exists as a class attribute" % name
            self.__class__.__dict__[name] = value
        else:
            print "Can't create new attributes, 'cos I said so!"



But this prevents setting attributes during initialization,
so it does not meet the spec.
Cheers,
Alan





More information about the Python-list mailing list