0

I am trying to return all product reviews by user name.

I have written a generator function but on coming back to it, it does not work and returns a syntax error:



def find_reviews(username):
    for u in reviewers_dicts:
        if u['username'] == username:
            review = u['reviews']
            yield review
            print(review)
            next

find_reviews('krpz1113')

I am getting the following error:

<generator object find_reviews at 0x0000016C852CECF0>

1 Answers1

2

<generator object find_reviews at 0x0000016C852CECF0> Is not an error, but a generator object (As it says in the output). To get your desired result, you can use next(). For example:

def func(x):
    num = 0
    while num < x:
        yield num
        num += 1
b = func(10)
# Use these to get an individual value
next(b) 
next(b) 

Output:

0
1

Alternatively, you can call the list method on your generator object:

def func(x):
    num = 0
    while num < x:
        yield num
        num += 1
b = func(10)
# Use this to get a list
lst = list(b)
print(lst)

or iterate through a for loop:

def func(x):
    num = 0
    while num < x:
        yield num
        num += 1
b = func(10)

for item in b:
    print(item)

krmogi
  • 2,588
  • 1
  • 10
  • 26