I want to print to a file using print
I import from __future___
. I have the following as an import:
from __future__ import print_function
From now on, I can print using:
print("stuff", file=my_handle)
However, I have many calls to print
in a function, so I would want to be able to use a function where the keyword argument is bound to my_handle
. So, I use partial application:
printfile = partial(print, file=my_handle)
printfile("stuff")
printfile("more stuff")
which is what I intended. However, is there any way I can change to definition of print
itself by partially applying the keyword argument? What I have tried was:
print = partial(print, file=my_handle)
however I got an error saying:
UnboundLocalError: local variable 'print' referenced before assignment
Is there any way to use print
without mentioning my file every time?