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?
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?
logging.info("NEW_SCORE : " + str(score))
Proof by Python interpreter:
>>> x = None
>>> "x: " + x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'NoneType' objects
>>> "x: " + str(x)
'x: None'
QED
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).
For print purpose,you need to str first. A comma is to print with a single space between it..For example:
print "hi guys","how are you today"
this syntax will output:
hi guys how are you today
but it will be different if your syntax like this:
print "hi guys"+"how are you today"
this syntax will output:
hi guyshow are you today