~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI, Writer of Tidbits, and Linux Enthusiast

Quick Python: __all__

Published on

Updated on

Warning: This post has not been modified for over 2 years. For technical posts, make sure that it is still relevant.

Anything that is defined inside a package that is imported is also brought into that workspace. So for example, if package A uses numpy and you import A, then A.numpy will be shown in your workspace. In order to limit the variables exported, you can define the ones you want to be shown in a variable called __all__.

Inside your package test.py

hidden_variable = 5
shown_variable = 2
__all__ = ['shown_variable']

Then if you open the standard Python REPL and import test, only the variable shown_variable will be seen.


Have any questions or want to chat: Reply via Email

Enjoyed this post?

Published a response to this? :