Sorry, another simple question

Randall Hopper aa8vb at yahoo.com
Thu May 20 10:52:16 EDT 1999


Joseph Kuan:
 |	How to derive the range function for a particular class?
 |
 |I tried with __range__ but it doesn't work.

Joseph, do you want to do something like this?

    mylist = MyList()

    for element in mylist:
      ...

Or something like this:

    mylist = MyList()

    print mylist[5:10]

If the former, __getitem__ is the method you need.  It's called with
successively increasing index values in the for loop until an IndexError
exception is triggered (IIRC):

    http://www.python.org/doc/current/ref/sequence-types.html

If the latter, look at __getslice__:

    http://www.python.org/doc/current/ref/sequence-methods.html

Randall

   




More information about the Python-list mailing list