[Python-Dev] Indexing builtin sequences with objects which supply
__int__
Tim Peters
tim.one@comcast.net
Sun, 23 Jun 2002 14:45:30 -0400
[Todd Miller, wants to use rank-0 arrays as regular old indices]
[Tim]
> Here's a sick idea: given Python 2.2, you *could* make the type
> of a rank-0 array a subclass of Python's int type
[Todd]
> Right now, numarray is a subclass of object for Python-2.2 in order to
> get properties in order to emulate some of Numeric's attributes. I'm
> wondering what I'd loose from object in order to pick up int's indexing.
All types in 2.2 inherit from object, including int.
>>> class IntWithA(int):
... def seta(self, value):
... self._a = value
... def geta(self):
... return self._a * 2
... a = property(geta, seta)
...
>>> i = IntWithA(42)
>>> i
42
>>> i.a = 333
>>> i.a
666
>>> range(50)[i]
42
>>>
So, e.g., adding arbitrary properties should be a crawl in the park.
> I'm also wondering how to make a rank-0 Float array fail as an index.
Quit while you're ahead <wink>. The obvious idea is to make a
Rank0FloatArray type which is not a subclass of int.