# When to create a generator or return simply a list, closed for missing context

The other day we got the question: When to create a generator or return simply a list in Python?

This question has a generic title, some code which contains enough code to be reviewed. And a couple of questions on ways they think the code could be improved.

It is closed with the Lacks concrete context close reason:

Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site.

The only time a closer mentioned missing context was when they said:

Depends on the context, which is missing in this question. Please take a look at the help center.

As the missing context wasn't specified, I asked that closer what context is missing, but I didn't get a reply.

And so, what context is missing that makes this question off-topic?

I was the first to vote to close, so I'll try to explain it.

First of all, it's hard to specify missing context. It's missing. It's the whole lack of something specified that's the problem.

The question consists of a single function, list_records which apparantly is mainly called (but not exlusively so) using a list comprehension ( [list_records(race_type, year) for foo in bar]). It might be doing something with a database (utils.dataracetype_to_DBcolumn) and we don't have the foggiest what the input data looks like. Neither do we know the expected output.

We even can't say for sure this is the actual code in use. Phrases like "this kind of instance method" and "I am calling it mainly through list comprehension" make me wonder about the bigger picture. What data is OP after exactly?

Q:

I am calling it mainly through list comprehension, but I could return it simply as a list, what is the most pythonic way to do this? What would be the reasons to chose one method or the other?

A:

Depends on the context, which is missing in this question. Please take a look at the help center.

Gareth, being a heck of a lot smarter and more experienced in Python than me, managed to answer it anyway:

If you are going to need the whole list for some reason, then make a list. But if you only need one item at a time, use a generator.

Exactly, we don't know what OP wants to have and what he wants to do with it. A reviewer shouldn't have to guess. Too often we get questions which fail to specify something which in hindsight turns out to be very important, rendering the answers (partially) useless. I voted to close because that was a definite possibility with this question.

Hence the request for context. Preferably as much as possible, but there's such a thing as asking too much. Example input, output and a comment about what portion of the resulting data is actually useful to the OP would've been enough. I was either too absent-minded or in too much of a hurry to specify further, I guess. It's been an intense week, my apologies to anyone affected.

• Do I understand you correctly, that we should know the input, the output, the function, utils.dataracetype_to_DBcolumn, and how it's used? – Peilonrayz Jul 4 '18 at 11:35
• @Peilonrayz Yes. If only a part of those are given it could be a grey area. – Mast Jul 4 '18 at 12:17
• If you write the boiler plate surrounding the function PyCharm can statically infer the types. Some of the types are Any, which are only passed to and from utils.dateracetype_to_DBcolumn. And so we can reasonably find the input and output. The OP describes how it's commonly used, "I am calling it mainly through list comprehension", if you need more you can ask the OP for this. But I don't think we need the OP to explain how list comprehensions work. And so to me it seems like you're hating on a question for having one undefined function. – Peilonrayz Jul 4 '18 at 12:35
• @Peilonrayz Actually I think it would be a great question once the context is added. But even the imports are missing and we can't be expected to rely on an IDE to understand a question. – Mast Jul 4 '18 at 12:42
• My point about the IDE is anyone with it and a little knowledge of Python knows what the input can be. Alternately you can whitebox test the program if you know a little more about Python. – Peilonrayz Jul 4 '18 at 13:34