A generator is simply a function which returns an object on which you can call next, such that for every call it returns some value, until it raises a StopIteration exception, signaling that all values have been generated. Such an object is called an iterator.
>>> def myGen(n):
... yield n
... yield n + 1
...
>>> g = myGen(6)
I quoted this from Understanding Generators in Python?
Here is what I am trying to figure out:
Which is the generator? myGen
or myGen(6)
?
According to the quote mentioned above, I think the generator should be myGen
. And myGen(6)
is the returned iterator object. But I am really not sure about it.
When I tried this:
>>> type(myGen)
<type 'function'>
>>> type(g) # <1>this is confusing me.
<type 'generator'>
>>> callable(g) # <2> g is not callable.
False
>>> callable(myGen)
True
>>> g is iter(g) # <3> so g should an iterable and an iterator
True # at the same time. And it will be passed as an argument
>>> for i in g: # to built-in function `next()` in a "for...in..." loop.
print i # (is that correct?)
6
7
So, according to <1>
and <2>
, g
's type is 'generator' and it is not callable.
But generators are callable, and calling a generator gets you an iterator object
What's going on here?
When I was searching for answers, I run into Every time you define a function python creates a callable object.
So, can I say something like this? when the function myGen
is defined, myGen
is a name referring to a callable object which is an instance of a class that has a __call__
method.
In this case, myGen
is a generator, and myGen(6)
is the returned iterator when myGen
is called.
But why does type(g)
return <type 'generator'>
at all?
And this returned iterator
thing also looks suspicious to me since there is no return
statement in the function.
Isn't it that Functions always return something (at least None
, when no return-statement was reached during execution and the end of the function is reached)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…