Extended slicing and Ellipsis - where are they used?

James Stroud jstroud at mbi.ucla.edu
Fri Sep 14 19:29:52 EDT 2007


Paddy wrote:
> And the ellipses ... ?

;)


py> class Bob(dict):
...   def __getitem__(self, k, *args, **kwargs):
...     if k is Ellipsis:
...       return sorted(self.keys())
...     else:
...       return dict.__getitem__(self, k, *args, **kwargs)
...   def __setitem__(self, k, *args, **kwargs):
...     if k is Ellipsis:
...       raise KeyError, "Can't make elliptical assignments."
...     else:
...       return dict.__setitem__(self, k, *args, **kwargs)
...
py> b = Bob(a=1, b=2, c=3, d=15.5)
py> b
{'a': 1, 'b': 2, 'c': 3, 'd': 15.5}
py> for k in b[...]:
   print '%s ==> %s' % (k, b[k])
...
a ==> 1
b ==> 2
c ==> 3
d ==> 15.5
py> b[...] = 2
------------------------------------------------------------
Traceback (most recent call last):
   File "<ipython console>", line 1, in <module>
   File "<ipython console>", line 9, in __setitem__
<type 'exceptions.KeyError'>: "Can't make elliptical assignments."



More information about the Python-list mailing list