I was talking to a friend about the Python coding conventions and suddenly said, "In Python's PEP8, it says that function names should be written in lowercase, but why some libraries ([ logging
](https: // docs) .python.org/ja/3/library/logging.html) etc.) Is camelCase adopted? "
#An example of logging code. The method name such as getLogger is camelCase.
import logging
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
In fact, PEP8 "Names of Functions and Variables" says:
Function names should be in lowercase only. Also, words should be separated by underscores as needed for readability.
Variable names follow the same conventions as functions.
Allow mixedCase for compatibility only if mixedCase is already in use (eg threading.py).
It says, "I'm sorry that camelCase is already used in some libraries." I was a little worried, so I tried to summarize the circumstances around that. To conclude first,
That was the reason.
Stackoverflow's "[How come the Python's logging module doesn't follow PEP8 conventions?](Https://stackoverflow.com/questions/22993667/how-come-the-pythons-logging-module-doesnt-follow-pep8-conventions?" As I learned from the answer of "/ 22993896 # 22993896)", it seems that logging
was originally implemented with reference to Java's Log4j
. It was also confirmed that PEP282 actually listed it as an "affected library".
If you look at the code for Log4j
in TECHSCORE article, it's almost the same.
//Instance generation
Logger logger=Logger.getInstance(Sample.class.getName());
//Set to generate log event when log level is WARN or higher
logger.setLevel(Level.WARN);
//Log output
logger.info("This is info.");//Not output
logger.warn("This is warn.");//Output
logger.error("This is error.");//Output
The reason is also written that "logging was already developed in 2001, and it was around the same time that PEP8 was formulated."
Also, according to the wiki, it seems that it violates PEP8 even among developers, but it is consistent within the project and backwards. It seems that it has not been fixed so far with an emphasis on compatibility.
API uses camelCase (goes against PEP8 recommendation and most of the stdlib)
- 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.
- 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
This wiki mentions a library called logging2
. Here, it is written as much as "better than log4j" w
A More Pythonic Logging System; or, You Deserve Better Than log4j
Also check out threading
, which was mentioned as an example of" when mixedCase is already in use "in PEP8. I did. In Python 3.8 documentation, it is written as "camelCase is also adopted for backward compatibility".
Although not listed here, the camelCase name used for some methods and functions in this module in the Python 2.x series is still supported in this module.
In Python2.7, it was written like this.
Starting with Python 2.6, this module provides a PEP 8-compliant alias that replaces the properties of camelCase affected by the Java threading API. This updated API is compatible with that of the multiprocessing module. However, the name camelCase has not been scheduled to be deprecated and will continue to be supported in both Python 2.x and 3.x.
This is gradually migrating in a PEP8 compliant form while maintaining compatibility. I hope the logging
library will gradually move to a PEP8 compliant form, just like threading
.
Also, according to the Documentation (https://docs.python.org/ja/3/library/threading.html), this library also seems to be strongly influenced by Java.
The rough design of this module is based on the Java threading model. That said, Java makes locks and condition variables the basic behavior of all objects, while Python separates them into separate objects.