为什么Python的日志记录模块不遵循PEP8约定? [英] How come the Python's logging module doesn't follow PEP8 conventions?
问题描述
这只是出于历史目的的好奇心:
This is is just a curiosity with historical purposes:
我想知道是否有人知道为什么使用非常广泛的(和核心模块) PEP -8命名约定.
I was wondering if someone knows why the very widely used (and core module) logging doesn't follow the Python's PEP-8 naming convention.
例如,在
>>> import logging
>>> log = logging.getLogger("hello")
我希望它是get_logger
,但不是.
当涉及到函数名称时,PEP8标准说:
When it comes to function names, the PEP8 standard says:
mixedCase仅在已经是 流行的样式(例如threading.py),以向后保留 兼容性.
mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.
是这样吗?如果是这样,它还必须与其他logging
保持向后兼容性吗?还是仅仅是logging
的开发人员感觉要使用驼峰式命名方式?
Was that the case? If so, with what other logging
thingy it had to maintain backwards compatibility? Or was it just that the developers of logging
felt like using camel-case naming?
当然,该模块文档齐全,一点也不重要.我很好奇.
Of course, the module is well documented and is not a big deal at all. I'm just curious.
推荐答案
logging
模块由也是getLogger()
方法.
The logging
module was developed by a separate company in 2001, and was heavily based on Log4j. As such it follows the naming conventions the original author picked, which mirror the Log4j choices; the latter has a getLogger()
method too.
直到一年后, PEP 282 提议将其添加到标准库,那时命名约定是固定不变的.
Not until a year later did PEP 282 propose to add it to the standard library, by which time the naming convention was set in stone.
这是该软件包的已知问题,但这不是唯一违反的软件包PEP.从链接的Wiki:
It is a known issue with the package, but it is not the only package to violate the PEP. From the linked Wiki:
PEP8说-与该样式指南的一致性很重要.项目内的一致性更重要.一个模块或功能的一致性至关重要.
PEP8 says - consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.
- 很正确,但是由于向后兼容,所以不能更改.可能是logging2. -techtonik
- 现在这不是一个低优先级的问题,除非有计划确保stdlib的其余部分都符合PEP8. -VinaySajip
- So True, but can't not be changed, because of backward compatibility. logging2 maybe. -- techtonik
- It's a low priority right now, unless there's an initiative to ensure the rest of the stdlib is made to conform to PEP8. -- VinaySajip
Last but not least, the styleguide itself has this to say on applying styleguides:
愚蠢的一致性是小头脑的妖精
样式指南是关于一致性的.与该样式指南的一致性很重要.项目内的一致性更重要.一个模块或功能的一致性至关重要.
A Foolish Consistency is the Hobgoblin of Little Minds
A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.
但最重要的是:知道何时不一致-有时样式指南就不适用.如有疑问,请运用您的最佳判断.查看其他示例,然后确定最适合的示例.而且不要犹豫,要问!
But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!
特别是:不要为了遵守本PEP而破坏向后兼容性!
In particular: do not break backwards compatibility just to comply with this PEP!
修复"
logging
会破坏向后兼容性,这是不值得的.'Fixing'
logging
would break backwards compatibility, which is just not worth it.这篇关于为什么Python的日志记录模块不遵循PEP8约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!