Limitations to subclassing builtin types (FAQ 6.6)

Gerrit Holl gerrit at
Sun Feb 9 13:48:31 CET 2003

Gerrit Holl schreef op zondag  9 februari om 11:45:35 +0000:
> Gerrit Holl schreef op zondag  9 februari om 09:48:41 +0000:
> > Laura Creighton schreef op zondag  9 februari om 03:20:32 +0000:
> > > Can you write up a better FAQ entry based on your experiences?
> > I can describe the behaviour, but I can't describe why it behaves as such.
> Yes I can, because I found the answer.

I found it partially. I have written this FAQ entry. I'd appreciate it
when someone could check it for errors, although most is copied from

Q: 6.6. Why can't I derive a class from built-in types (e.g. lists or files)?
As of Python 2.2, you can derive from built-in types. It is restricted, however.

  *	You can use multiple inheritance, but you can't multiply inherit from
	different built-in types (for example, you can't create a type that
	inherits from both the built-in dict and list types). This is a
	permanent restriction; it would require too many changes to Python's
	object implementation to lift it. However, you can create mix-in classes
	by inheriting from "object". This is a new built-in, naming the
	featureless base type of all built-in types under the new system.

  *	When using multiple inheritance, you can mix classic classes and
	built-in types (or types derived from built-in types) in the list of
	base classes. (This is new in Python 2.2b2; in earlier versions you

  *	Some types can't be subclassed:
  		* Types with a fixed number of instances: BooleanType,
		  EllipsisType, NoneType, NotImplementedType.
		* Callables: FunctionType, MethodType, ClassType, GeneratorType,
		  and related
		* Internal types: CodeType, TracebackType, SliceType, FrameType
		* Further: BufferType, DictProxyType, XRangeType.

More information on types, old and new, can be found at:

For previous versions, the answer is:

This is caused by the relatively late addition of (user-defined) classes to the language -- the implementation framework doesn't easily allow it. See the answer to question 4.2 for a work-around.

Remaining questions:
	* Why can't types with a fixed number of instances be subclassed?
	* Why can't callables be subclassed?
	* Why can't internal types be subclassed?
	* Why can't BufferType, DictProxyType and XRangeType be subclassed?

Is the answer to these questions the same as with multiple inheritance,
that 'it would require too many changes to Python's object implementation'?
Although I wouldn't know when I'd _want_ to subclass any of these, it would
be cool to understand...

28 >>> def f():
28 ...  gelukt = []
28 ...  mislukt = []
28 ...  for t in dir(types):
28 ...   try: exec "class A(types.%s): pass" % t
28 ...   except: mislukt.append(t)
28 ...   else: gelukt.append(t)
28 ...  return mislukt, gelukt
29 >>> f()
(['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'CodeType', 'DictProxyType', 'EllipsisType', 'FrameType', 'FunctionType', 'GeneratorType', 'InstanceType', 'LambdaType', 'MethodType', 'NoneType', 'SliceType', 'StringTypes', 'TracebackType', 'UnboundMethodType', 'XRangeType', '__builtins__', '__doc__', '__file__', '__name__'], ['ComplexType', 'DictType', 'DictionaryType', 'FileType', 'FloatType', 'IntType', 'ListType', 'LongType', 'ModuleType', 'ObjectType', 'StringType', 'TupleType', 'TypeType', 'UnicodeType'])


Asperger Syndroom - een persoonlijke benadering:
Het zijn tijden om je zelf met politiek te bemoeien:

More information about the Python-list mailing list