decorator to prevent adding attributes to class?

Michele Simionato michele.simionato at gmail.com
Sat Jul 12 00:00:06 EDT 2008


On Jul 11, 9:24 pm, Neal Becker <ndbeck... at gmail.com> wrote:
> Robert Bossy wrote:
> > 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.

It can with a metaclass redefining the __call__ method
to call freeze after instantation. But there
would a lot of magic going on such a design.



More information about the Python-list mailing list