Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __all__ = ['bar', 'baz']
- waz = 5
- bar = 10
- def baz(): return 'baz'
- from foo import *
- print bar
- print baz
- # The following will trigger an exception, as "waz" is not exported by the module
- print waz
- __all__ = ['foo', 'Bar']
- from module import * # imports foo and Bar
- package/
- |-__init__.py # makes directory a Python package
- |-module_1.py
- |-module_2.py
- from module_1 import *
- from module_2 import *
- __all__ = ['foo',]
- __all__ = ['Bar',]
- import package
- package.foo()
- package.Bar()
- package/
- |-__init__.py
- |-module_1/
- | |-__init__.py
- | |-foo_implementation.py
- |-module_2/
- |-__init__.py
- |-Bar_implementation.py
- from foo_implementation import *
- __all__ = ['foo']
- from Bar_implementation import *
- __all__ = ['Bar']
- from Bar_implementation import *
- from Baz_implementation import *
- __all__ = ['Bar', 'Baz']
- from module_1 import * # also constrained by __all__'s
- from module_2 import * # in the __init__.py's
- __all__ = ['foo', 'Bar'] # further constraining the names advertised
- import package
- package.Baz()
- from module_1 import *
- from module_2 import *
- __all__ = ['foo', 'Bar', 'Baz']
- import os as _os, sys as _sys
- import sys
- def export(fn):
- mod = sys.modules[fn.__module__]
- if hasattr(mod, '__all__'):
- mod.__all__.append(fn.__name__)
- else:
- mod.__all__ = [fn.__name__]
- return fn
- $ cat > main.py
- from lib import export
- __all__ = [] # optional - we create a list if __all__ is not there.
- @export
- def foo(): pass
- @export
- def bar():
- 'bar'
- def main():
- print('main')
- if __name__ == '__main__':
- main()
- $ cat > run.py
- import main
- main.main()
- $ python run.py
- main
- $ cat > run.py
- from main import *
- foo()
- bar()
- main() # expected to error here, not exported
- $ python run.py
- Traceback (most recent call last):
- File "run.py", line 4, in <module>
- main() # expected to error here, not exported
- NameError: name 'main' is not defined
- a = "A"
- b = "B"
- c = "C"
- __all__ = ['a', 'b']
- a = "A"
- b = "B"
- c = "C"
- __all__
- __all__
- """ cheese.py """
- __all__ = ['swiss', 'cheddar']
- swiss = 4.99
- cheddar = 3.99
- gouda = 10.99
- >>> from cheese import *
- >>> swiss, cheddar
- (4.99, 3.99)
- >>> gouda
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- NameError: name 'gouda' is not defined
- >>> import cheese
- >>> cheese.swiss, cheese.cheddar, cheese.gouda
- (4.99, 3.99, 10.99)
- >>> from cheese import swiss, cheddar, gouda
- >>> swiss, cheddar, gouda
- (4.99, 3.99, 10.99)
- >>> import cheese as ch
- >>> ch.swiss, ch.cheddar, ch.gouda
- (4.99, 3.99, 10.99)
- __all__ = [
- 'MySQLConnection', 'Connect', 'custom_error_exception',
- # Some useful constants
- 'FieldType', 'FieldFlag', 'ClientFlag', 'CharacterSet', 'RefreshOption',
- 'HAVE_CEXT',
- # Error handling
- 'Error', 'Warning',
- ...etc...
- ]
- foo
- ├── bar.py
- └── __init__.py
Add Comment
Please, Sign In to add comment