Unified Type/class

Pedro Rodriguez pedro_rodriguez at club-internet.fr
Sun Jan 27 14:26:03 EST 2002


"Alex Martelli" <aleax at aleax.it> wrote:

> Pedro Rodriguez wrote:
>         ...
>>> I'm not sure that it's a problem in Python that the capability is
>>> restricted to classes coded in Python. In cases where you need to
>         ...
>> But I think that you can even restrict this capability on classes by
>> providing the apropriate metaclass, something like 'FinalType' with a
>> semantic close to 'final' keyword in Java.
> 
> Sure -- piece of cake [warning, untested code]:
> 
> class finalType(type):
>     def __init__(self, name, bases, classdict):
>         if bases: raise TypeError, "cannot subclass Final type"
>         type.__init__(self, name, bases, classdict)
> 
> Just use __metaclass__=finalType in a class body, and that class won't
> be subclassable (except by further metaclass tweaks...:-). As above
> written, the class itself also won't be able to subclass any other, but
> that's easy to remedy too, e.g.:
> 
> class finalType2(type):
>     def __init__(self, name, bases, classdict):
>         if bases and isinstance(bases[0], finalType2):
>             raise TypeError, "cannot subclass Final type [2]"
>         type.__init__(self, name, bases, classdict)
> 
> 
> Alex
> 

<wink>
Let me think. 
I only have 2 wishes left...
Now I would like : the singleton, the enumeration metaclasses.
Even the Borg pattern will be meta-assimiled ;)
... correction, this has already happened, in 'descrintro'
</wink>

-- 

Pedro





More information about the Python-list mailing list