Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7164785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:07:58+00:00 2026-05-28T14:07:58+00:00

I have a function (f) with a yield . I have a list (E)

  • 0

I have a function (f) with a yield. I have a list (E).

If I try E += f() then f() doesn’t return a generator object, but instead runs the function, which throws an exception because some global variables aren’t ready.

To forestall any comments, I know E += f() is wrong, but it provides an example for my question. If I do the right thing E += [f()] or E.append(f()) then f() returns a generator object and the code of f() is not evaluated till the generator object’s next method is called.

My question is, why is f() being evaluated in the wrong situation at all, and why instead is the exception raised not something along the lines of “function object not iterable”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:07:58+00:00Added an answer on May 28, 2026 at 2:07 pm

    Because the expression f() calls f, and E += f() will run the generator until its end and append the values it generates to E. It’s not “wrong” at all, but it does require a fully working f that yields a finite number of values:

    >>> E = [1,2,3]
    >>> def f():
    ...  yield 4
    ...  yield 5
    ...
    >>> E += f()
    >>> E
    [1, 2, 3, 4, 5]
    

    By contrast, E += [f()] will call f, but then stops before the first line of the code because then the interpreter has a complete enough object, namely the generator object, to store in the single-element list [f()]. Only later, when you request more elements from E[-1], will code up to the first yield run and the exception be raised.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which can return a list of items or a single
I have a function that returns a list via yield. I use this function
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have a function that gets x(a value) and xs(a list) and removes all
How do you name a function that returns a generator (that is, uses yield
I'm using a python generator function to provide me with a list of images
Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():
I have a function in python whose output is a generator : def main_function(x):
Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar'
I have a function that gets a list of DB tables as parameter, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.