I am trying to create a generator function to return the content of a .csv file row by row and while the generator function does seem to be iterable with me being able to loop over it with a for loop, when I print the object type of the generator function, instead of returning class ‘generator’, it returns class ‘function’. The generator function also has a memory size consistent with it being a generator function.
The function code:
JavaScript
x
14
14
1
import sys, time
2
3
start_time = time.time()
4
def file_row_generator():
5
for row in open('file.csv'):
6
yield row
7
print(file_row_generator)
8
print(type(file_row_generator))
9
print(sys.getsizeof(file_row_generator))
10
l = []
11
for row in file_row_generator():
12
l.append(row)
13
print(time.time() - start_time)
14
This returns the output:
<function file_row_generator at 0x00F3BC40>
<class ‘function’>
68
0.05980682373046875
Advertisement
Answer
file_row_generator
is a function that returns a generator. Try print(file_row_generator())
.
JavaScript
1
9
1
>>> def foo():
2
for i in range(3):
3
yield i
4
5
>>> print(foo)
6
<function foo at 0x7fd3cedaa310>
7
>>> print(foo())
8
<generator object foo at 0x7fd3ced88660>
9