A couple questions about classes and inheritance
Terry Reedy
tjreedy at udel.edu
Mon May 17 12:56:12 EDT 2010
On 5/16/2010 10:52 PM, Mark Young wrote:
> You can't subclass Ellipsis.
You clipped what I believe you were responding to that I posted:
"I believe that in 3.1, the builtin classes with builtin names can be
subclassed and and those without cannot. (If you find any exceptionss,
please post ;-). The ones with names are exactly the ones you are
expected to directly interact with in normal code."
Ellipsis is not an exception to what I said but rather an example that
supports my point. It is not a class, but an instance of an
intentionally *unnamed* class which one cannot subclass. It is unnamed
in the sense of not being bound to a name in a namespace (builtins),
even though, like all classes, it has a 'definition' or name-attribute name.
>>> Ellipsis
Ellipsis
>>> type(Ellipsis)
<class 'ellipsis'>
>>> class C(type(Ellipsis)): pass
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
class C(type(Ellipsis)): pass
TypeError: type 'ellipsis' is not an acceptable base type
The class of None is also unnamed, while the class of True and False,
bool, is named so we can call it. It does turn out that bool *is* an
exception to the general rule.
>>> class C(bool): pass
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
class C(bool): pass
TypeError: type 'bool' is not an acceptable base type
If there were a practical reason to subclass it, a subclass of int
instead should do just as well.
Terry Jan Reedy
More information about the Python-list
mailing list