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
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.