I'm trying to write a replacement for the print() function; I'd like to understand what Python 3 is doing here.
Consider the following two Python 3 source files:
file.py:
def in_a_file(): print("this is in a file") minimal.py:
import builtins from file import * def test(): print("this is a test") def printA(*args, **kwargs): builtins.print("A: ", end="") builtins.print(*args, **kwargs) print = printA test() in_a_file() When I run minimal.py, I get this output:
A: this is a test this is in a file So, clearly, the assignment print=printA has changed the behavior of print() in function test(), but not in function file().
Why?
And, is there code I could put in minimal.py that would similarly change the behavior of print() in function file()?
printin your file is not the same as the one infile.py. This is done so that module authors don't have to worry that their variables might conflict with variables in the caller.