Editable strings anyone?

Brian Kelley bkelley at wi.mit.edu
Tue Feb 19 16:15:32 EST 2002


John Machin wrote:

> 
> You'll find one nuisance when working with 'c' arrays; it doesn't
> support coercing an ordinary string to an array -- you have to do it
> yourself. For example:
> 
> x = array.array('c', 'abc')
> x = x + 'def'
> 
> doesn't work. You have to do 
> 
> x = x  + array.array('c', 'def')
> 


I encountered the same problem and of course the solution was subclass 
array.array!  Unfortunately, array.array can't be subclassed.  (Python 
2.2)  In fact, I found arraymodule.c very confusing in this respect.

It turns out that array.array is a function that returns an instance of 
type array.ArrayType which isn't so bad except that it thinks that it is 
of type array.array

This got a little bit confusing when I initially tried to subclass 
array.array

 >>> x = array.array('c', 'adc')
 >>> type(x)
<type 'array.array'>
 >>> type(array.array)
<type 'builtin_function_or_method'>

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

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


What would it take to make array.array or array.ArrayType an acceptable 
base type for subclassing?


Brian Kelley




More information about the Python-list mailing list