decorator to prevent adding attributes to class?

rbossy at jouy.inra.fr rbossy at jouy.inra.fr
Sat Jul 12 05:07:23 EDT 2008


>> class Foo(Freezeable):
>> def __init__(self):
>> self.bar = 42
>> self.freeze() # ok, we set all variables, no more from here
>>
>>
>> x = Foo()
>> print x.bar
>> x.bar = -42
>> print x.bar
>> x.baz = "OMG! A typo!"
>>
>
>Pretty nice, but unfortunately the subclass has to remember to call freeze
>in it's init.  Too bad that can't be automated.

Not to mention that subclasses constructors could be bit, like that:

class Foo(Freezeable):
    ...

class Bar(Foo):
    def __init__(self, *args, **kw):
        Foo.__init__(self, *args, **kw)
        self.something_bar = 42 # this should raise AttributeError


Cheers
RB



More information about the Python-list mailing list