decorator to prevent adding attributes to class?

Michele Simionato michele.simionato at
Fri Jul 11 19:03:55 CEST 2008

On Jul 11, 6:38 pm, Robert Bossy
> I don't get it. Why use a metaclass? Wouldn't the following be the same,
> but easier to grasp:
> class Frozen(object):
>     def __setattr__(self, name, value):
>        if not hasattr(self, name):
>           raise AttributeError, "cannot add attributes to %s" % self
>        object.__setattr__(self, name, value)

This is easier, but it does not stop the user from
adding class level attributes: this is the job
of the metaclass. If you don't care about
class level attributes (including methods,
properties, etc) and you are content with restricting
only the instance attributes your recipe is fine, yes.

More information about the Python-list mailing list