0

To have a list of logging levels in Python 2 I have to call this:

logging._levelNames.keys()

While Python 3 logging module has this:

list(logging._nameToLevel)

I am just adding six to my project to provide compatibility between Python 2 and Python 3. What is the cleanest way to figure out this compatibility issue?

4

1 回答 1

1

您不应故意访问以 a开头的属性_,这通常意味着它是私有属性,不应在其他地方使用。

而且由于日志记录级别只是一个简短的列表,您可以复制并粘贴并创建自己的列表:

levels = ['CRITICAL', 'FATAL', 'ERROR', 'WARN', 'WARNING', 'INFO', 'DEBUG', 'NOTSET']

顺便说一句,如果您注意到logging._levelNames.keys()list(logging._nameToLevel)提供不同的结果,前者还包含一些整数[0, 10, 20, 30, 40, 50],而后者不包含。

于 2017-06-24T06:07:56.433 回答