steve at holdenweb.com
Wed Dec 24 13:59:17 CET 2008
James Stroud wrote:
> Steven Woody wrote:
>> In the book Python Essential Reference, Chapter 3, when talking about
>> extended slicing, it gives an example: a = m[0:10, 3:20]. But I
>> don't understand how the 'm' was defined. What should it looks like?
> m could be an instance of the Krayzee class.
> py> class Krayzee(object):
> ... def __getitem__(self, i):
> ... try:
> ... r = ['WTF?' for j in i]
> ... except:
> ... r = 'WTF?'
> ... return r
> py> m = Krayzee()
> py> m[1:2:3, 4:5:6]
> ['WTF?', 'WTF?']
> py> m['your moms']
> ['WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?', 'WTF?']
I'm not sure what this is supposed to prove. It might be more helpful to
show what's actually going on ...
>>> class k(object):
... def __getitem__(self, i):
... r = [j for j in i]
... except Exception, e:
... print i, ":", e
... r = i
... return r
>>> m = k()
>>> m[1:2:3, 4:5:6]
[slice(1, 2, 3), slice(4, 5, 6)]
['h', 'e', 'l', 'p', '!']
As you can see, no exceptions are raised here, and the x:y:z notation
introduces a slice object, which the code doesn't handle in any way
shape or form.
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list