Python23: cannot inherit from bool?

Carl Banks imbosol at vt.edu
Wed Jan 15 02:52:57 EST 2003


Martin v. L?wis wrote:
> Carl Banks <imbosol at vt.edu> writes:
> 
>> This reminds me of a question I've been pondering, in case anyone
>> knows: Is it the plan that built-in singleton objects (None, Ellipsis,
>> NotImplemented, ?) will eventually get a new metatype?  
> 
> To my knowledge, no.
> 
>> Although it seems like there would be small benefit for built-in
>> objects (the only tangible thing I can think of is you could ask a
>> singleton type for its object), it could be used for Python types,
>> too.
> 
> What specific functionality would you expect to see in this metatype?
> 
> For a C-implemented type, all you have to do is to leave out the
> tp_new slot, then creating new instances by calling the type will
> cause TypeErrors.
> 
> For a Python-implemented type (i.e. a newstyle class), not being able
> to call the class to create an instance is not helpful: how would you
> then create the single instance?

Simple.  The singleton metatype has an additional slot which points to
the instance.  The instance is created when the class is created, and
accessed through an attribute, like so:


    class NoneType:
        __metaclass__ = SingletonType

    None = NoneType.object



-- 
CARL BANKS




More information about the Python-list mailing list