decorator to prevent adding attributes to class?
michele.simionato at gmail.com
Fri Jul 11 17:45:27 CEST 2008
On Jul 11, 5:29 pm, Neal Becker <ndbeck... at gmail.com> wrote:
> After spending the morning debugging where I had misspelled the name of an
> attribute (thus adding a new attr instead of updating an existing one), I
> would like a way to decorate a class so that attributes cannot be (easily)
> I guess class decorators are not available yet (pep 3129), but probably
> inheritance can be used.
> Can anyone suggest an implementation?
This article could give you same idea (it is doing the opposite,
if an attribute is overridden):
There is also a recipe that does exactly what you want by means of a
It is so short I can write it down here:
# requires Python 2.2+
"Raise an error when trying to set an undeclared name."
raise AttributeError("You cannot add attributes to %s" %
"""Subclasses of Frozen are frozen, i.e. it is impossibile to add
new attributes to them and their instances."""
Of course using frozen classes is not Pythonic at all, and I wrote the
a proof of concept, not to use it.
More information about the Python-list