I have this need to make everything into a parameter. Here’s some code I wrote recently:
import subprocess, tempfile
Returns the text typed in the editor, after running strip().
with tempfile.NamedTemporaryFile() as t:
subprocess.call([os.environ.get('EDITOR', 'vi'), t.name])
There’s a voice in my head that says I shouldn’t be importing the subprocess and tempfile modules outside my function and then referring to them from within. And, the voice goes on, it wouldn’t be OK to move the import within my function either. Instead, if my function needs to use
NamedTemporaryFile, I should pass that function in as a parameter.
I think it all starts with the fact that I studied economics in college. I had a lot of lectures that started with a professor drawing something like this on the chalk board:
labor supply = f(...)
And then during the lecture, she would slowly replace the ellipses with parameters. By the end of class, the function might look like:
labor supply = f(income, wealth)
Then I’d also have a few pages of notes explaining the nature of the relationship between how hard somebody is willing to work, the wage they can earn, and the wealth they already have. The thing that sunk in deep is the idea that parameters (and only parameters) are what drives the dependent variable on the left hand site. Anything that drives labor supply is listed as a parameter. If it ain’t on the right hand side, then it is not relevant.
By the way, the war between the wealth effect and the income effect is one of the areas of economics that really does explain our behavior pretty dang well, and there’s neat charts involved, so go read about it.