Search code examples
pythonif-statementdebuggingcomments

how to enable or disable many print statements in python with log?


the code looks like this - has many prints for debug purposes. I want to have some toggle 'button' , one liner that enables/disables those print statements. without commenting out/in.

#age
            if (row['age'] < 25.0):
                print()
                print('age < 25')
                score += -45.0
                print('score changed to : %d ' % score)

            elif (row['age'] >= 25.0) and (row['age'] < 29.0):
                print()
                print('25 < age < 29')
                score += -22.0
                print('score changed to : %d ' % score)

            elif (row['age'] >= 29.0) and (row['age'] < 35.0):
                print()
                print('29 < age < 35')
                score += 1.0
                print('score changed to : %d ' % score)

            elif (row['age'] >= 35.0):
                print()
                print('age > 35')
                score += 19.0
                print('score changed to : %d ' % score)

            #f27
            if (row['f27']== ''):                
                print()
                print('f27 missing found')
                score += -18.0
                print('score changed to : %d ' % score)

Solution

  • Hope code below will help you.

    from __future__ import print_function
    debug  = True
    def print(*args, **kwargs):
         if(debug):
                 return __builtin__.print(*args, **kwargs)
    

    The above code will override the builtin print function. it prints if debug set to True otherwise it skips print.