2

I have a code piece that works on generators and generator functions. When I try to slice the generators with itertools.islice, the code piece generates no outputs.

I've looked into the code, and found out the following code piece:

if isinstance(result, dict):
    self.returned(result)
elif inspect.isgenerator(result):
    for x in result:
        self.returned(x)
else:
    self.returned(result)

It turns out that inspect.isgenerator returns False for itertools.islice, which is what breaks the code. inspect.isgeneratorfunction behaves the same.

  1. Isn't itertools.islice a generator, or an generator function?
  2. How can I find out of result is a generator OR an itertools.islice object?
iTayb
  • 12,373
  • 24
  • 81
  • 135

1 Answers1

1

Based on

inspect.getmembers(itertools.islice)

islice is an iterator, not a generator. More info Difference between Python's Generators and Iterators

Community
  • 1
  • 1
bbaja42
  • 2,099
  • 18
  • 34
  • `islice` is both an iterator and a generator. Take a look at it's code here: http://docs.python.org/library/itertools.html#itertools.islice – iTayb Oct 19 '12 at 17:52
  • 4
    @iTayb I believe that is just a Python representation of what is going on under the hood, not how it is actually implemented – RocketDonkey Oct 19 '12 at 17:53