1

Suppose I have 2 functions and one global variable in a python file. How do I allow to export only one function?

g = 47
def A():
    print('a')

def B():
    print('b')

Suppose the above file is named as try.py. I want to restrict all imports from try module except function A.

Faizan
  • 23
  • 4
  • 2
    prefix your private function name with an underscore that marks them as private (just a practice), there is no way to prevent it from importing. – WSMathias9 Sep 09 '21 at 15:01

1 Answers1

1

Use __all__:

__all__ = ["stuff", "to", "export"]

<your code>

Not that if something undefined is in __all__, AttributeError is raised.

Alan Bagel
  • 818
  • 5
  • 24