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.

1
  • 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. Commented Sep 9, 2021 at 15:01

1 Answer 1

1

Use __all__:

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

<your code>

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

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

1 Comment

Note that __all__ doesn't prevent the import of anything…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.