when x = range(10), x stores a generator range(10).
I wonder then does ‘x’ have all the numbers between 0~9?
This concept quite eludes me because a generator is an ‘action’ that produces a number one by one, then when storing it into a variable and returning it produces what? My question originates from the code below.
def csv_reader(file_name): file = open(file_name) result = file.read().split("\n") return result
My first understanding of the code was, since ‘open()’ is a generator that yields line by line, ‘result’ would have simply the ‘first line’ of the whole text. But it seems result stores the ‘whole’ file and runs into ‘MemoryError’.
Also, as for the code below,
def csv_reader(file_name): for row in open(file_name, "r"): yield row
‘open()’ is a generator that has a ‘yield’ statement, and there is also a ‘yield’ statement in the function. Then does the function end up having a double ‘yield’ statement?
I just found out a curious thing about range. It is not an ‘iterator’?