Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it PEP8 compliant to import CamelCase as lowercase?

Tags:

python

pep8

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.

like image 973
Elbenfreund Avatar asked Jun 02 '16 10:06

Elbenfreund


People also ask

Can I use CamelCase in Python?

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.

What are PEP8 standards?

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.

Which choice is PEP 8 compliant as the name of a class?

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.

Which of the following styles does PEP8 recommend for multi word variable names?

As mentioned, PEP 8 says to use lower_case_with_underscores for variables, methods and functions.


2 Answers

Gtk is a class not a module. Class names should use CapWords convention.

You can read more about that here.

like image 94
Tomasz Plaskota Avatar answered Oct 16 '22 08:10

Tomasz Plaskota


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()
like image 32
Dilettant Avatar answered Oct 16 '22 09:10

Dilettant