If a function takes as an input the name of a text file, I can refactor it to instead take a file object (I call it “stream”; is there a better word?). The advantages are obvious – a function that takes a stream as an argument is:
- much easier to write a unit test for, since I don’t need to create a temporary file just for the test
- more flexible, since I can use it in situations where I somehow already have the contents of the file in a variable
Are there any disadvantages to streams? Or should I always refactor a function from a file name argument to a stream argument (assuming, of course, the file is text-only)?
There are numerous functions in the python standard library which accept both — strings which are filenames or open file objects (I assume that’s what you’re referring to as a “stream”). It’s really not hard to create a decorator that you can use to make your functions accept either one.
One serious drawback to using “streams” is that you pass it to your function and then your function reads from it — effectively changing it’s state. Depending on your program, recovering that state could be messy if it’s necessary. (e.g. you might need to litter you code with
f.tell()and thenf.seek().)