tags:

views:

111

answers:

4
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.

+1  A: 

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.

nosklo
Cryptic is too kind a euphemism for "illiterate."
Seth Johnson
@Seth - come on. the OP tried, but I'd say that English isn't the native language. "illiterate" is a bit harsh
marcc
+2  A: 

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> _
paxdiablo
hi,paxdiablo,the code 1 make dead-loop also.
zjm1126
@zjm1126, I think you're after the second variant. Modifying the answer to make that clear.
paxdiablo
+1  A: 

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
Mark Byers
@Person who downvoted this answer: why?
Mark Byers
Saying nothing against @paxdiablo; this was the first correct answer.
Adam Bernier
@Adam Bernier: out of interest - how can you see which answer was first? All I see is 'answered 1 hour ago' for both. I know that the exact timestamps will appear tomorrow, but how would I find that out now. (PS: I already know I was first, I'm just curious how you would investigate it).
Mark Byers
Hmm... I just sorted by 'newest' and mine comes before paxdiablo. I guess that's how to find out. But Andrea Ambu comes before me, though his answer was not as complete as mine, and he later edited it. I can't see how to tell whether he edited his answer before or after I submitted mine. Why don't SO just print the timestamps?!
Mark Byers
@Mark Byers: yeah, for now, hover your mouse over the relative times. In this case, of course I also took into account the time @paxdiablo edited his answer.
Adam Bernier
@Adam: Ah, thank you. That is an excellent tip (about the mouseover)! I hadn't noticed that.
Mark Byers
A: 

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
Andrea Ambu