Extended slicing and Ellipsis - where are they used?
Paddy
paddy3118 at googlemail.com
Sat Sep 15 02:44:32 EDT 2007
On Sep 15, 12:29 am, James Stroud <jstr... at mbi.ucla.edu> wrote:
> 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."
Thanks again James.
More information about the Python-list
mailing list