Good way of handling NoneType objects when printing in Python

python, string

Solution

The best approach is:

logging.info("NEW_SCORE: %s", score)

In most contexts, you'd have to use a `%` operator between the format string on the left and the value(s) on the right (in a tuple, if more than one). But the `logging` functions are special: you pass the format string as the first argument, then, one after the other, just as many arguments as needed to match the number of `%s` &c formatting markers in the format, and the `logging` functions will use the formatting operator `%s` as appropriate if and only if necessary -- so you don't incur any runtime overhead if your current logging level is such that, e.g., `logging.info` is not actually going to be shown.

Forget `str` calls and `+`-based string concatenation anyway -- even without `logging`'s specials, `%`-formatting is really the way to go (in Python 2.6 or earlier; in 2.6 or later, you should also consider strings' `format` method, allowing clearer and more readable expression of what amounts to the same functionality).

Problem

How do I go about printin a NoneType object in Python? ``` # score can be a NonType object logging.info("NEW_SCORE : "+score) ``` Also why is that sometime I see a comma instead of the + above?

Original source