May-31-2021, 02:59 PM
(This post was last modified: May-31-2021, 03:12 PM by deanhystad.)
Containing yield makes a function a generator. And since the yield forces the function to be a generator you need to use next or for to get the values and you cannot use return in the function. You could write your function like this:
def get_ep_info(iter=False): ep_list_info = list() ep_nums = [1,2,3,4,5] for ep_num in ep_nums: if iter is True: print("true") yield ep_num elif iter is False: print("false") ep_list_info.append(ep_num) if iter is False: print("false") yield ep_list_info #<- Change from return to yeild a = next(get_ep_info()) print(a)But it makes far more sense to leave it a generator and use list() to create a list of the returned values.
def get_ep_info(): for ep_num in [1,2,3,4,5]: yield ep_num a = list(get_ep_info()) print(a)This is less likely to result in uncontrolled laughter or swearing when people try to use your code