Python23: cannot inherit from bool?
Greg Chapman
glc at well.com
Thu Jan 16 10:34:36 EST 2003
On 16 Jan 2003 11:13:07 +0100, martin at v.loewis.de (Martin v. Löwis) wrote:
>Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> writes:
>
>> >.... don't know the answer to that one, but is the non inheritance
>> >property available to ordinary python if yes we could create leaf
>> >classes.
>>
>> At the risk of sounding dim: what's a leaf class?
>
>I think the OP meant this as a synonym for what others call "final"
>classes, i.e. classes that cannot be used as a base class. The OP asks
>whether it is possible to implement a Python class which gives an
>error when it is used as a base. I believe the answer to that question
>is "no".
>
Wouldn't something like this work?:
LeafClass = None #initialized so that it exists when LeafClass itself
#is being created
class LeafMetaClass(type):
def __new__(metaclass, name, bases, attrs):
if LeafClass is not None:
for base in bases:
if hasattr(base, '_leafclass_'):
raise TypeError('cannot inherit from '+base.__name__)
attrs['_leafclass_'] = True
return super(LeafMetaClass, metaclass).__new__(metaclass, name,
bases, attrs)
class LeafClass(object):
'''Can serve as a mix-in base class to declare that it is illegal to
inherit from a class'''
__metaclass__ = LeafMetaClass
---
Greg Chapman
More information about the Python-list
mailing list