2

When I have a single file module relying on a 3rd party module or method from a 3rd party module which is imported in the single file module, then I have access to the 3rd party module or method when I import the single file module. I want to prevent that, my suggestion was to import the 3rd party module or method and renaming them with an underscore in front. Would that be a good idea, what other options do I have.

Example 1:

# this is a single file module called DemoModule.py
import numpy as np


def calculate_something():
    x = np.cos(0)

    return x

Now when I import DemoModule I do not only have access to calculate_something, but I also have access to everything numpy has to offer as well. Would this be a good way to avoid that situation:

# this is a single file module called DemoModule.py
import numpy as _np


def calculate_something():
    x = _np.cos(0)

    return x

Example 2:

# this is a single file module called DemoModule.py

from numpy import cos


def calculate_something():
    x = cos(0)

    return x

Now when I import DemoModule I do not only have access to calculate_something, but I also have access to cos. Would this be a good way to avoid that situation:

# this is a single file module called DemoModule.py

from numpy import cos as _cos


def calculate_something():
    x = _cos(0)

    return x

Edit:

A run from the command line

In[2]: import DemoModule
In[3]: DemoModule.np.cos(1)
Out[3]: 0.54030230586813977
15
  • 2
    I am missing the use case for this. The module doing the importing is going to get access to numpy if they want it. What problem are you fixing? Commented Apr 5, 2018 at 13:38
  • 1
    You are aware that using underscores for private variables is only a python naming convention, it does not change how anything functions Commented Apr 5, 2018 at 13:40
  • 2
    I realize you want to prevent that. What I am unclear on, is what problem do you think will be fixed when you do that. Commented Apr 5, 2018 at 13:41
  • 1
    Maybe i'm missing the point, but when you import DemoModule, do you actually have access to numpy? Commented Apr 5, 2018 at 13:44
  • 1
    relevant stackoverflow.com/questions/28766414/… Commented Apr 5, 2018 at 14:04

1 Answer 1

3

First the usual disclaimer: Python has no concept of enforced "privacy" - the single leading underscore is only a naming convention saying "let's pretend you don't know this exists" (I assume you know this already but better to make things clear right from the start).

And the canonical answer is : define the __all__ attribute of your module with the names you want to publicly expose - every other name in the module should then be considered by users as "private". You can double this with a single-leading-underscore (import foo as _foo) if you really want to make the point clear but that's perhaps a bit over the top.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.