Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leading underscore before the name of Python module

Tags:

python

I have searched around for this but I did not find any answer. I'm looking at a Python code written by someone else and there is a module say "GUI_Module". This module contains a class which contains GUI related methods. The module is then imported in the __main__ Python file which makes use of those methods.

Instead of being imported like this with its actual name:

import GUI_Module

it is imported like this with a leading underscore:

import _GUI_Module

Now I know about most meanings of underscores in Python but I have not found anything that explains what a single underscore before the name of a module that we are importing is supposed to do.

like image 817
Cantaff0rd Avatar asked Mar 14 '19 08:03

Cantaff0rd


People also ask

What is leading underscore in Python?

A single leading underscore in front of a variable, a function, or a method name means that these objects are used internally. This is more of a syntax hint to the programmer and is not enforced by the Python interpreter which means that these objects can still be accessed in one way on another from another script.

What does _ and __ mean in Python?

The use of double underscore ( __ ) in front of a name (specifically a method name) is not a convention; it has a specific meaning to the interpreter. Python mangles these names and it is used to avoid name clashes with names defined by subclasses.

What does _ before a variable mean in Python?

The underscore prefix is meant as a hint to another programmer that a variable or method starting with a single underscore is intended for internal use. This convention is defined in PEP 8. This isn't enforced by Python. Python does not have strong distinctions between “private” and “public” variables like Java does.

Why is __ used in Python?

Single standalone underscore _ is a valid character for a Python identifier, so it can be used as a variable name. According to Python doc, the special identifier _ is used in the interactive interpreter to store the result of the last evaluation. It is stored in the builtin module. Here is an example.


1 Answers

In the implementation of the standard library in CPython, the _ prefix seems to indicate that a module is "private", in the sense that you shouldn't import it directly. Usually, this is because its functionality is imported and exposed by some "public", documented module.

Such a private module may be an accelerator module - a fast C implementation that exists alongside a slower pure-Python implementation of the same module (and which the pure-Python version tries to import everything from, so that the faster version is always automatically used if available).

Not all modules implemented in C are accelerator modules, though. In some cases, the _-prefixed module written in C is the only implementation available. For instance, CPython doesn't contain a pure-Python implementation of the ctypes or sqlite modules; these modules respectively depend on importing stuff from the _ctypes or _sqlite3 modules (which are implemented in C) and hence can't be used by a Python interpreter that doesn't support Python's C API.

And not all _-prefixed modules are even implemented in C! For example, the _pydecimal module is a pure-Python implementation of the decimal module. The implementation of decimal in CPython tries to import _decimal (implemented in C) if it's available, and if not, falls back to importing _pydecimal.

The only thing that seems to be universally true of these _-prefixed modules is that they're all implementation details of some documented, "public" module that doesn't have a _-prefix, and that you're supposed to use that module instead of directly importing stuff from the _-prefixed one.

like image 92
Mark Amery Avatar answered Sep 24 '22 04:09

Mark Amery