title: Loop over the indices of a slice
body: <p>I'm trying to implement the <code>__getitem__</code> method for a class in python. I want to try something like... </p>
  
  <pre><code>def __getitem__(self, key):
    if isinstance(key, int):
      return foo(key)
    elif isinstance(key, slice):
      return [foo(k) for k in key]
    else:
      raise TypeError("Invalid index.")
  </code></pre>
  
  <p>...but I get a <code>'slice' object is not iterable</code> error whenever I actually try to use it with a slice. What's the correct way to do this in python?</p>