According to PEP8 modules should be lowercase. Some popular ones out there (e.g. Gtk) however follow the CamelCase convention.
In order to have a pythonic codebase and mitigate spillage of this policy breach it seems like the following is a clean way to deal with this:
import CamelcasedModule as camelcased_module
Linters such as pep8-naming however claim that such practice violates PEP8 and throw an N813
error.
As I failed to find any direct passage in PEP8 addressing this I was wondering which way to go in order to stay true to the zen of python.
Sitenote:
Previously this question mentioned Gtk
as an example:
from gi.repository import Gtk as gtk
Which was misleading as Gtk
is a class not a module and as such does not apply to the question. For transparency and because the answers to this may still be usefull it is mentioned here.
You clipped an important part of PEP8: "mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility." Sometimes, CamelCase is acceptable.
PEP 8, sometimes spelled PEP8 or PEP-8, is a document that provides guidelines and best practices on how to write Python code. It was written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The primary focus of PEP 8 is to improve the readability and consistency of Python code.
I try to adhere to the style guide for Python code (also known as PEP 8). Accordingly, the preferred way to name a class is using CamelCase: Almost without exception, class names use the CapWords convention.
As mentioned, PEP 8 says to use lower_case_with_underscores for variables, methods and functions.
Gtk
is a class not a module. Class names should use CapWords convention.
You can read more about that here.
I second the answer of Tomasz Plaskota that this indeed is an anti-pattern and I like to amend:
In chapter/section "Public and internal interfaces" of pep8:
Imported names should always be considered an implementation detail. Other modules must not rely on indirect access to such imported names unless they are an explicitly documented part of the containing module's API, such as os.path or a package's
__init__
module that exposes functionality from submodules.
Otherwise it is clear, that even the standard library cleans up steadily making classes CamelCase and modules lower_underscore_case ...
At first I only skimmed over the question and stored more the trial of:
import Gtk as gtk
The aliasing on import I often do use - but more in the classical local shortcut use case:
import very_impressive_hierarchical_name_for_tools as our_tools
or as in the famous self overwriting fun case of the datetime module with the datetime class:
import datetime as dt
to then be able in actual client code using like:
a_datetime = dt.datetime.now()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With