在 Python 中打印时处理 NoneType 对象的好方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1338690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:59:57  来源:igfitidea点击:

Good way of handling NoneType objects when printing in Python

pythonstring

提问by biznez

How do I go about printin a NoneType object in Python?

如何在 Python 中打印 NoneType 对象?

# score can be a NonType object
logging.info("NEW_SCORE : "+score)

Also why is that sometime I see a comma instead of the + above?

另外,为什么有时我会看到逗号而不是上面的 +?

回答by Alex Martelli

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 loggingfunctions 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 loggingfunctions will use the formatting operator %sas appropriate if and only ifnecessary -- so you don't incur any runtime overhead if your current logging level is such that, e.g., logging.infois not actually going to be shown.

在大多数情况下,您必须%在左侧的格式字符串和右侧的值之间使用运算符(在元组中,如果有多个)。但是这些logging函数是特殊的:您将格式字符串作为第一个参数传递,然后一个接一个地传递与格式中%s&c 格式标记的数量匹配所需的尽可能多的参数,并且这些logging函数将使用格式运算符%s作为当且仅在必要时才合适——因此,如果您当前的日志记录级别是这样的,例如,logging.info实际上不会显示,则不会产生任何运行时开销。

Forget strcalls 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' formatmethod, allowing clearer and more readable expression of what amounts to the same functionality).

无论如何忘记str调用和+基于字符串的连接——即使没有logging's 的特殊性,%-formatting 确实是要走的路(在 Python 2.6 或更早版本中;在 2.6 或更高版本中,您还应该考虑字符串的format方法,允许更清晰、更易读的表达式什么相当于相同的功能)。

回答by Andrew Keeton

logging.info("NEW_SCORE : " + str(score))

Proof by Python interpreter:

Python解释器的证明:

>>> 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

QED

回答by user149513

For print purpose,you need to str first. A comma is to print with a single space between it..For example:

出于打印目的,您需要先 str 。逗号是在它之间打印一个空格..例如:

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

嗨,伙计们,今天你好吗

回答by kanghai

if not score==None: logging.info("NEW_SCORE : "+score)

如果不是 score==None: logging.info("NEW_SCORE : "+score)

or

或者

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

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