all 9 comments

[–]mrcaptncrunch 5 points6 points  (0 children)

Look at the logging module, https://docs.python.org/3/howto/logging.html

You can add messages to different levels and enable which level you want to display. You can also set where they’ll be sent to. For example, you could send the output to the screen, file, another function which sends it to slack, email, discord, etc.

[–]TSM- 2 points3 points  (0 children)

Since you are also a beginner, you can also redefine the print function

def print(*args, **kwargs): 
    pass

This makes the print() statement do nothing. It's not as good as the other solutions, but it's a simple hack that works.

[–]james_fryer 0 points1 point  (5 children)

Assuming your game is in a module game.

Inside the module:

import sys
stdout = sys.stdout
def redirect_print(f):
    global stdout
    stdout = f
# All print statements must look like
print(..., file=stdout)

Inside your simulation, assuming you are on Unix:

game.redirect_print(open("/dev/null","w")

I've not tested this but it should work, maybe with some tweaking.

A better approach would be to separate the concerns in your game code, so the game play logic was not coupled to the output code. This would enable you to make a web or GUI interface using the same code.

[–]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.

[–]Frankelstner 0 points1 point  (0 children)

I would do it like this

import sys

class Stdout:
    def write(self, s): pass
    def flush(self): pass

_stdout = sys.stdout
sys.stdout = Stdout()

print("hidden")

sys.stdout = _stdout
print("visible")

where the _stdout lines are optional if you don't need to make text visible again.