Deriving from builtin types with limited instances and Extending FAQ 6.6 
Author Message
 Deriving from builtin types with limited instances and Extending FAQ 6.6

Hi,

FAQ 6.6 is:

Quote:
> 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://www.*-*-*.com/ ~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
        http://www.*-*-*.com/



Thu, 28 Jul 2005 04:34:15 GMT  
 Deriving from builtin types with limited instances and Extending FAQ 6.6
   ...

Quote:
>  >>> 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:
>>> type(iter)

<type 'builtin_function_or_method'>
Quote:
>>> type(map)

<type 'builtin_function_or_method'>

Quote:
>>> type(int)
<type 'type'>
>>> type(list)
<type 'type'>

This is exactly the difference: you can inherit from a TYPE (if
that type is designed to allow inheritance, and says so among
the flags in its C-level structure) but you can never inherit
from a FUNCTION.

Alex



Thu, 28 Jul 2005 23:19:41 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Deriving from builtin types with limited instances and Extending FAQ 6.6

2. Limitations to subclassing builtin types (FAQ 6.6)

3. Instance data, CVF 6.6 COM Server application

4. Derived types with derived types with allocatables

5. Derived-type subcomponents of derived type

6. FYI: Extended Tcl and Tcl 6.6 / Tk 3.1

7. Array.extend versus instance.extend

8. Type conversions forced by derived type: alternative?

9. Deriving abstract types from abstract types

10. allowed derived type component types

11. Derived types containing components of PRIVATE type

12. Extension of non-limited type needs limited component

 

 
Powered by phpBB® Forum Software