Conversation started Nov 1, 2019 at 11:00.
Nov 1, 2019 11:00
def mygen():
    print("one")
    yield 10
    print("two")
    yield 20
    print("three")
    yield 30
    print("four")

f = mygen()

for i in f:
    print(i)
D:\rhs\Python>python test.py
one
10
two
20
three
30
four
so here:
when u run the for loop
Suppose you have an iterator f. Don't worry about how the iterator got created.
it runs the function
There are two ways to step through an iterator:
1. use a for loop. The loop steps through the iterator each time through the loop.
OK so far?
2. use the next(f) function.
Every time you call next(f) it returns the next value from the iterator.
Nov 1, 2019 11:03
ok
Let's take a concrete example. Suppose you have an iterator f that returns 1 then 2 then 3 then 4.
So if we do:
for i in f:
  print(i)
This will print:
1
2
3
4
OK so far?
iterator is same as iterable?
is list iterator?
when you iterate through a list Python quietly defines an iterator that steps through the list. You never actually get to see this.
Lists are not themselves iterators.
ok
@JohnRennie Suppose you have an iterator f that returns 1 then 2 then 3 then 4.
Whar do you mean by this
the iterator contains(1,2,3) right
so when u call ,it returns them?
one by one
We need to be clear about the terminology. An iterator is an object that steps through some data.
The iterator doesn't necessarily contain the data though it can do.
Suppose you had a list l=[1,2,3] and you define a function:
def foo(l):
    for i in len(l):
        yield l[i]
Nov 1, 2019 11:12
ok
This defines a generator called foo and when you call foo(x) it returns an iterator that steps through the list x.
So we have two different objects:
1. we have the list `x`
2. we have the iterator that steps through the list
The iterator is not the same as the iterable object.
This would normally be the case. The mygen generator used as an example is unusual in that the data is actually contained inside the generator.
Nov 1, 2019 11:15
ok
how would replacing for with next work here
Suppose we have an iterator f, then next(f) returns the first value that the iterator produces.
so 1
Then when we call next(f) again it returns the second value that the iterator produces.
are generators and iterators same
last doubt
When you call a generator it retruns an iterator.
In your original code we have a generator called mygen, then did:
f = mygen()
This makes f the iterator.
Nov 1, 2019 11:22
ok
Then we can do for i in f or i = next(f) to get the data from the iterator f.
def foo(l):
    for i in range(len(l)):
        yield l[i]

x = ["rats", "cats", "bats"]
f = foo(x)

for i in f:
    print(i)
foo(l) is generator
x is the data i.e. a list. foo is the generator and f is the iterator.
Nov 1, 2019 11:30
@JohnRennie okay got t
it
so next(f) is same as for loop
@JohnRennie One final doubt
def mygen():
    	print("one")
    	yield 10
    	print("two")
    	yield 20
    	print("three")
    	yield 30
    	print("four")

f=mygen()

res=next(f)
print(res)

res=next(f)
print(res)

res=next(f)
print(res
so mygen() is generator
Nov 1, 2019 11:32
f is iterator
so when u do res=next(f)
why it also shows one
print doesn't return anything
Because behind the scenes the iterator f is calling the generator function mygen, and that contains print statements.
The print statements don't return anything they just print. It's the yield commands that return the values in the iterator.
yeah but u are printing (res)
A real generator would not have print statements in it. I guess they have been put in to show that the iterator is calling the function mygen.
Nov 1, 2019 11:37
@JohnRennie So is res a variable
When you call next(f) that get the next value from the iterator f
@JohnRennie Aha got it
The iterator f gets that value from the generator mygen
And the generator mygen has print statements in it.
next(f) doesn't contain print
it contains 10,20,30
Nov 1, 2019 11:41
@JohnRennie So iterator is stepping though what data?yield(10),yield(20)?
print(one)..these are not what iterator steps through
Yes, the iterator gets each yield value in turn.
aha got it
The print statements don't affect the data returned by the iterator at all.
yeah
so i guess iterator teps through both print and yield
but it returns yield
am i right
 
Conversation ended Nov 1, 2019 at 11:46.