[Python-Dev] Indexing builtin sequences with objects which supply __int__

Todd Miller jmiller@stsci.edu
Fri, 21 Jun 2002 12:00:27 -0400


Guido van Rossum wrote:

>>I liked MvL's __index__ method. I understand your hesitancy. It's
>>pretty tough exploring new features side-by-side the "version
>>fatigue" thread :)
>>
>
>Yes.
>
>>>What's your use case?  Why do you need this?
>>>
>
>>This might settle it :) Numeric/numarray arrays are sometimes used in 
>>reduction operations (e.g. max) which eliminate one dimension. Sometimes 
>>the result is a zero dimensional array, which is currently converted to 
>>a Python scalar in both Numeric and numarray. The conversion to scalar 
>>enables integer zero dimensional results to be used as indices, but 
>>causes other problems since any auxilliary information in the array 
>>(e.g. type = Int8) is lost. Adding some form of implicit conversion to 
>>index value might permit us to retain zero dimensional objects as arrays.
>>
>
>But when you're indexing Numeric/numarray arrays, you have full
>control over the interpretation of indices, so you can do this
>yourself.  
>
Yes.  We do this now in numarray.

>Do you really need to be able to index Python sequences
>(lists, tuples) with your 0-dimensional arrays?
>
We want to.  Here's why:

1) Currently, when you fully reduce or subscript a numarray, you get 
back a python scalar.  This has the disadvantages that:

a. information (type=Int8) is lost
b. precision (Float128 --> types.FloatType) can be lost.
c. subsequent code must handle multiple types:
 
   result = some_array_operation()
   if result in PythonScalarTypes:
      do_it_the_scalar_way()
   else:
      do_it_the_array_way()

2) 0-D arrays can be represented as simple scalars using __repr__.  This 
creates a convenient illusion that a 0-D array is just a number.  0-D 
arrays solve all of the problems cited in 1.  But 0-D arrays introduce 
one new problem:

a. 0-D arrays don't work as builtin sequence indices, destroying the 
illusion that what lies at the bottom of an array is just a number.  If 
a fix for this was conceivable,  we'd be willing to do the frontend work 
to make it happen.

>Could you live with
>having to call int() in those cases?
>
Yes and no.  I think there are two disadvantages:

a. There is a small notational overhead, and the need to remember it. 
 In terms of the illusion that a 0-D array is a number, this will be a 
point of confusion.

b. Once int() is added, the semantics of the code are narrower than they 
used to be.   The same code called with  an array as the sequence, might 
otherwise accept an index to be an array.  Once int() is used, this can 
no longer reasonably happen since int(multi_valued_array) should raise 
an exception.

Thanks for your attention on this thread.  Have a nice vacation!

Todd

>
>--Guido van Rossum (home page: http://www.python.org/~guido/)
>
>
>_______________________________________________
>Python-Dev mailing list
>Python-Dev@python.org
>http://mail.python.org/mailman/listinfo/python-dev
>


-- 
Todd Miller 			jmiller@stsci.edu
STSCI / SSG			(410) 338 4576