Can my own objects support tuple unpacking?

greg greg at cosc.canterbury.ac.nz
Fri Mar 28 19:59:22 EDT 2008


Scott David Daniels wrote:

>     class Foo(object): # _Never_ use old-style without a reason
>         def __getitem__(self, index):
>             print index
>             if index < 3:
>                 return index * 5  # just to see
>             raise IndexError('Zapped') # The secret -- run out.

Another way is to make your object iterable -- read
up about the "iterator protocol".

-- 
Greg



More information about the Python-list mailing list