gpt4 book ai didi

python - 为什么 Python 的日志记录模块不遵循 PEP8 约定?

转载 作者:IT老高 更新时间:2023-10-28 21:58:32 30 4
gpt4 key购买 nike

这只是出于历史目的的好奇:

我想知道是否有人知道为什么使用非常广泛(和核心模块)logging不遵循 Python 的 PEP-8 naming convention .

例如,在

>>> import logging
>>> log = logging.getLogger("hello")

我希望它是 get_logger,但它不是。

说到function names ,PEP8 标准说:

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

是这样吗?如果是这样,它必须使用什么其他 logging 东西来保持向后兼容性?还是只是 logging 的开发者喜欢使用驼峰命名法?

当然,该模块是有据可查的,根本不是什么大问题。我只是好奇。

最佳答案

logging 模块由 separate company 开发2001 年,主要基于 Log4j。因此,它遵循原作者选择的命名约定,反射(reflect)了 Log4j 的选择;后者有 getLogger() method too .

直到一年后 PEP 282建议将其添加到标准库中,到那时命名约定已经确定。

这是一个 known issue with the package ,但它不是唯一违反 PEP 的包。来自链接的 Wiki:

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.

  • 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

最后但同样重要的是,styleguide itself关于应用样式指南有这样的说法:

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!

In particular: do not break backwards compatibility just to comply with this PEP!

'Fixing' logging 会破坏向后兼容性,这是不值得的。

关于python - 为什么 Python 的日志记录模块不遵循 PEP8 约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22993667/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com