yes, with metaclasses (was Re: Read-only class definitions?)
Troels Therkelsen
t_therkelsen at hotmail.com
Fri Oct 11 08:27:42 EDT 2002
In article <xxxp9.32771$Fz.941846 at news1.tin.it>, Alex Martelli wrote:
> In 2.2.* (and later), a class IS a class instance -- the class of
> the class is also known as its _metaclass_. Inherit from the
> builtin type named 'type', override whatever you wish, and make
> your new metaclass the class's metaclass (by setting __metaclass__
> in class body, or inheriting from another class with that custom
> metaclass, etc).
>
> Example:
>
> class metaNoDel(type):
> def __delattr__(self, name):
> raise TypeError, "Can't delete attr %s from NoDel class!" % name
>
> class NoDel:
> __metaclass__ = metaNoDel
>
> class a(NoDel):
> def afunc(self):
> pass
>
> class b(a):
> pass
>
> c = b()
> c.afunc()
> del a.afunc
> c.afunc()
>
Alex,
Thanks a lot for the pointer on the __metaclass__, I had totally ignored that
aspect. This will be useful in another project I'm working on :-)
However, what would prevent the code from doing
del a.__metaclass__.__delattr__
del a.afunc
?
Regards,
Troels Therkelsen
More information about the Python-list
mailing list