1

Extending the question - Using print statements only to debug

I like this idea of a global debug/test variable. Also with a function you have a lot of flexibility like later on adding time etc.

DEBUG = True

def log(s):
    if DEBUG:
        print s

What are the Pros & Cons of using the print like this if I wanted to skip a function call:

DEBUG = True

print('Yellow') if DEBUG else None
user 923227
  • 2,528
  • 4
  • 27
  • 46
  • 1
    It's a useful feature. Not only for print statements. You might want to look at a pre-processor to take care of such a variable. See this: https://stackoverflow.com/questions/13352677/python-equivalent-for-ifdef-debug – SnowGroomer Jul 21 '20 at 23:05
  • 1
    Your approach to using the ternary conditional expression only works because of the defined short-circuit behavior, but a more conventional (and shorter) one-liner would be `if DEBUG: print("Yellow")` – Craig Jul 21 '20 at 23:14

3 Answers3

1

I have worked with many programs, in many programming languages, that took exactly this approach.

More generally, though, you'd prefer to use Python's logging facility to dispose of the messages, instead of the hard-to-control print-statement.

You also, as always, need to be careful that your "logging statements" don't inadvertently do something, such that the behavior of the program will change when you flip the switch off.

wovano
  • 4,543
  • 5
  • 22
  • 49
Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
0

It really depends on your requirements:

If all you want is to print a simple string like in your example, skipping a function call may be fine.

But what happens if you want to add metadata, like a timestamp, or a tag?

Wrapping your logging code in a function gives you the following advantages:

  • You can format the log output without rewriting the formatting code
  • You can easily have more than one type of loggin and choose which is active (error, warning, debug, info)
  • You can easily change out print for something else in the future if the need arises, like writing to file, or to stderr instead of stdout
  • Your code will be more clear, since it will be obvious what is log and what is output
  • You will have to type less

The example you provided is only good for trivial code and trivial debugging, but since you want to control debugging with a variable, clearly you have more than a trivial script.

So, if you still don't want to use proper logging library for some reason, at least, do wrap your logging code in a proper function as otherwise you are likely to reduce the readability and maintainability of your code.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
0

Pros:

  • It will be better for YOUR debugging

Cons:

  • You shouldnt use this in serious case like license verification
  • Logging with logger in python is much better for this cases and you can hide it as .file
  • Everyone else will find it not so good way of debugging because its quite amateur way
  • It will be worse to distinguish real output from debug if you dont make some format

Summary:

This way is acceptable for small amateur project, but i think you should consider using logger and start to using it for your future projects so you know how to use it. Using Debug variable can be usefull but remember it can be dangerous if you declare it multiple times or on wrong place.