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

Todd Miller jmiller@stsci.edu
Fri, 21 Jun 2002 09:42:02 -0400


  Guido van Rossum wrote:

>[Todd Miller]
>
>>>>There has been some recent interest in the Numeric/numarray community 
>>>>for using array objects as indices
>>>>for builtin sequences.  I know this has come up before, but to make 
>>>>myself clear, the basic idea is to make the
>>>>following work:
>>>>
>>>>class C:
>>>>   def __int__(self):
>>>>         return 5
>>>>
>>>>object = C()
>>>>
>>>>l = "Another feature..."
>>>>
>>>>print l[object]
>>>>"h"
>>>>
>>>>Are there any plans (or interest) for developing Python in this direction?
>>>>
>
>[Guido]
>
>>>I'm concerned that this will also make floats acceptable as indices
>>>(since they have an __int__ method) and this would cause atrocities
>>>like
>>>
>>>print "hello"[3.5]
>>>
>>>to work.
>>>
>
>[Todd]
>
>>That makes sense.    What if we specifically excluded Float objects from 
>>the conversion?   Are there any types that need to be excluded?    If 
>>
^ other types

>>
>>there's a chance of getting a patch for this accepted,  STSCI is willing 
>>to do the work.
>>
>
>Hm, an exception for a specific type seems ugly.  What if a user
>
I agree.

>
>defines a UserFloat type, or a Rational type, or a FixedPoint type,
>with an __int__ conversion?
>
Perry actually suggested excluding instances of any subclass of Float. I 
see now that there is
also the related problem of excluding instances which act like Floats.

>
>This points to an unfortunate early design flaw in Python (inherited
>from C casts): __int__ has two different meanings -- sometimes it
>converts the type, sometimes it also truncates the value.
>
>I suppose you could hack something where you extract x.__int__() and
>x.__float__() and compare the two, but that could lead to a lot of
>overhead.
>
Sounds too tricky to me. I'd hate to explain it.

>
>
>I hesitate to propose a new special method, but that may be the only
>solution. :-(
>
I liked MvL's __index__ method. I understand your hesitancy. It's pretty 
tough exploring new features side-by-side the "version fatigue" thread :)

>
>
>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.

>
>--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
>