subclassable types

W

Wolfgang Maier

Dear all,
I am wondering what the rules are that determine whether a built-in type is
subclassable or not.
As examples, why can you base your classes on int or set,
but not on bool or range?
Also: can you use introspection to find out whether a type is valid as a
base type?
Thanks for your help!
 
S

Steven D'Aprano

Dear all,
I am wondering what the rules are that determine whether a built-in type
is subclassable or not.

The type-designer thought it should, or shouldn't, be.

As examples, why can you base your classes on int or set, but not on
bool or range?

In the case of bool, because of the rule that bool is a singleton
(actually, "doubleton") class. Since a bool subclass would have
instances, that would break the rule that there is only one true bool and
one false bool.

As for range, I'm not sure. It may not be a deliberate decision, but a
side-effect of the implementation. I have a vague recollection of there
being a difference between "heap types" and "non-heap types", which has
probably got something to do with the C implementation.

If that's the case, I see that both Jython and IronPython have treated
this as a language restriction rather than an accident. IronPython
especially is interesting:
.... pass
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot derive from IronPython.Runtime.XRange because it is
sealed


Also: can you use introspection to find out whether a type is valid as a
base type?

I don't believe so, but welcome correction. I would just try. If it
succeeds, it will succeed, otherwise it will raise TypeError.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top