Deriving from builtin types with limited instances and Extending FAQ 6.6

Gerrit Holl gerrit at nl.linux.org
Sat Feb 8 15:34:15 EST 2003


Hi,

FAQ 6.6 is:

> 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. 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. This may be fixed in the (distant) future. 

However, this is incomplete.

 >>> class A(int): pass
 ...

Great, this one works, and can be very useful.

 >>> class A(bool): pass
 ...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: type 'bool' is not an acceptable base type

Why doesn't this one work, I wondered when I first saw it. I found an
answer: BooleanType has limited instances, as has NoneType. However:

 >>> class A(iter): pass
 ...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: cannot create 'builtin_function_or_method' instances

This one doesn't work either. The error message seems reasonable,
becuase 'class A(map)' won't work either. However, what is exactly
the difference with 'class A(int)'? I can also try:

 >>> def f(): yield 1
 ...
 >>> class A(f().__class__): pass
 ...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: type 'generator' is not an acceptable base type

This offers no solution.  Of course, a real-life example
is always difficult to give, but the design is unclear to me. The
same is true for deriving for FunctionType. Another thing which
doesn't seem consequent to me:

 >>> class A(int, list): pass
 ...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: multiple bases have instance lay-out conflict

Why not consider both int and list as classes, and simply apply
the rules for multiple-inheritance, although it may have strange
consequences? The design is unclear for me, and the FAQ is very
incomplete.

May this be fixed in the (distant) future?

yours,
Gerrit.

-- 
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list