Skip to content

Instantly share code, notes, and snippets.

@heethesh
Created March 4, 2019 16:33
Show Gist options
  • Save heethesh/e0317d405d96d906b478c8400715f61e to your computer and use it in GitHub Desktop.
Save heethesh/e0317d405d96d906b478c8400715f61e to your computer and use it in GitHub Desktop.
import os
import sys
def block_printing(func):
def func_wrapper(*args, **kwargs):
# Block all printing to the console
sys.stdout = open(os.devnull, 'w')
sys.stderr = open(os.devnull, 'w')
# Function call
value = func(*args, **kwargs)
# Enable all printing to the console
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
return value
return func_wrapper
@heethesh
Copy link
Author

heethesh commented Mar 4, 2019

Usage

@block_printing
def foo(a):
    print(a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment