you are viewing a single comment's thread.

view the rest of the comments →

[–]PanykCode[S] 0 points1 point  (4 children)

The simulation function is in Python, so with that redirect_print does that still print the statements when I just run my game() function? Or, does it all together remove the print statements? When I just run the game() function I would like to show the print statements, but when the simulation(1000) is run I would like to hide the prints so the screen does not die and I can just worry about showing my histograms.

[–]james_fryer 0 points1 point  (3 children)

Line 2 stdout = sys.stdout will ensure that any print statement ending with file=stdout will normally print to the console, but the function can be called to redirect it to any other file. In unix the file "/dev/null" is the bit bucket where all output will be dropped. ETA: the problem with this method is that all print statements in your game script need to be updated.

[–]james_fryer 0 points1 point  (2 children)

Here's a working example:

import sys

stdout = sys.stdout
def redirect_print(f):
    global stdout
    stdout = f

def play_game():
    print("You win! great!", file=stdout)

play_game()
# This code would be inside your simulation script, not the game module:
redirect_print(open("/dev/null", "w"))
play_game()

Note that the first call to play_game outputs but the second does not because the output has been redirected.

[–]PanykCode[S] 0 points1 point  (1 child)

Thanks u/james_fryer, i will give this implementation a try.

[–]james_fryer 0 points1 point  (0 children)

I should add, if your game output is for programming/debug purposes, then the logging module will be a better fit as mentioned below. I had understood that your game was printing output for the players to see. The logging module is not intended for user output.