Question

I am working on a Python 3 program that will among other things write data to text files in a number of formats. I have one function per format, generating the data to be written. I see two possible ways of writing these functions - one using yield and the other using stream.write.

Consider this silly example, just writing an integer per line:

def numbers(max):
    for i in range(max):
        yield str(i) + "\n"

with open(file, "w") as f:
    f.writelines(numbers(10))
def numbers(max, stream):
    for i in range(max):
        stream.write(i + "\n")

with open(file, "w") as f:
    numbers(10, f)

I find the first version to make for a nicer function (e.g. one parameter less), but perhaps the code consuming it is somewhat more convoluted. But I also find it easier to test, since I don't need to construct a dummy stream class that captures the output.

Is any of these better than the other, or am I just getting stuck on an unimportant decision? My priorities are to produce readable and pythonic code.

Was it helpful?

Solution

Since you're going to use a form of write for writing either way, the real question is "Should I separate iteration from output or not?"

In general, yes, you should separate concerns if they are as different in nature as computation and I/O. If you keep working with this code base, you will almost inevitably encounter situations where you need to print something other than "all items in the iterable", or do something other than printing with the items. Therefore, having separate functions for these two is not over-engineering, it is simply good factoring.

Licensed under: CC-BY-SA with attribution
scroll top