0

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.

A. K.
  • 34,395
  • 15
  • 52
  • 89

1 Answers1

1

how about this?

def foo():
    if __name__ != "__main__":
        print("cant run")

What does if __name__ == "__main__": do?

Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29