'How to prevent a function from being imported into another module in python
How can we restrict other modules from importing certain functions while allowing others.
Desired behavior
# module A: a.py
def foo():
print('foo')
def bar():
print('bar')
# module B: b.py
from a import foo # error, foo not found in a
from a import bar # okay!
EDIT:
When someone writes from a import foo
they should get error of some sorts. This way I can restrict people from importing functions that are meant to be only called from the functions in the same file.
Solution 1:[1]
how about this?
def foo():
if __name__ != "__main__":
print("cant run")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Freddy Mcloughlan |