def c():
yield 222
yield 333
a=[1,2,3,4]
b=iter(c,333)
print a,b
for i in b:
print i
how can i get it.
def c():
yield 222
yield 333
a=[1,2,3,4]
b=iter(c,333)
print a,b
for i in b:
print i
how can i get it.
You didn't call c().
Your question is cryptic. I don't know what you expect.
Please, edit the question and add information about what you thought that would do, and what you've observed instead.
You need to provide the function (which will return the next value) to your iter() call. In your case, that's c().next rather than c.
This snippet below works as expected by producing all the yielded values up to, but exclusive of, the terminating value.
def generator():
yield 1
yield 2
yield 3
yield -1
sequence = iter (generator().next, -1)
print sequence
for value in sequence:
print value
The output of that is:
pax> python prog1.py
<callable-iterator object at 0xb77dd6ac>
1
2
3
pax> _
iter takes a callable and a sentinel and calls the callable repeatedly. Calling c repeatedly creates new generators which is not what you want. You want to call c once and then repeatedly call the next function, so try this instead:
def c():
yield 222
yield 333
a=[1,2,3,4]
b=iter(c().next, 333)
print a,b
for i in b:
print i
Output:
222
Because c never returns 333.
When using a sentinel in iter the first argument must be a callable, and iter will yield the returning value of the first argument until this value is equal to the sentinel.
What you'd like to do should be something like:
def c():
yield 222
yield 333
a=[1,2,3,4]
b=iter(c().next,333)
print a,b
for i in b:
print i