Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Yielding ahead of time
#2
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
Daring_T likes this post
Reply


Messages In This Thread
Yielding ahead of time - by Daring_T - May-31-2021, 02:39 PM
RE: Yielding ahead of time - by deanhystad - May-31-2021, 02:59 PM
RE: Yielding ahead of time - by Daring_T - Jun-01-2021, 02:43 AM
RE: Yielding ahead of time - by BashBedlam - May-31-2021, 03:03 PM
RE: Yielding ahead of time - by deanhystad - May-31-2021, 03:21 PM
RE: Yielding ahead of time - by Daring_T - Jun-01-2021, 02:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  logging messages ahead of print messages vindo 6 3,283 Jun-18-2019, 02:45 PM
Last Post: vindo
  Issues with async and yielding from API GSerum 1 2,178 Dec-18-2018, 08:37 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020