Skip to content

Instantly share code, notes, and snippets.

@sbuss
Last active August 29, 2015 14:17
Show Gist options
  • Save sbuss/97b86b41014673b5de9b to your computer and use it in GitHub Desktop.
Save sbuss/97b86b41014673b5de9b to your computer and use it in GitHub Desktop.
Effect of __all__
(temp)sbuss@sbuss-counsyl:~/envs/temp/src$ mkdir my_package
(temp)sbuss@sbuss-counsyl:~/envs/temp/src$ touch my_package/__init__.py
(temp)sbuss@sbuss-counsyl:~/envs/temp/src$ cd my_package/
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ ls
__init__.py
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ mkdir foo
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ touch foo/__init__.py
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ touch foo/bar.py
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ touch baz.py
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ mkdir oof
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ touch oof/__init__.py
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ vim __init__.py
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ cat __init__.py
__all__ = [
'foo'
]
(temp)sbuss@sbuss-counsyl:~/envs/temp/src/my_package$ cd ..
(temp)sbuss@sbuss-counsyl:~/envs/temp/src$ ipython
Python 2.7.8 (default, Oct 19 2014, 16:03:53)
Type "copyright", "credits" or "license" for more information.
IPython 3.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from my_package import <tab>
baz foo oof
In [1]: from my_package import *
In [2]: who
foo
In [3]: baz
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-258622b16882> in <module>()
----> 1 baz
NameError: name 'baz' is not defined
In [4]: import my_package
In [5]: my_package.baz
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-8b7f7abcf26e> in <module>()
----> 1 my_package.baz
AttributeError: 'module' object has no attribute 'baz'
In [7]: from my_package import baz
In [8]: baz
Out[8]: <module 'my_package.baz' from 'my_package/baz.pyc'>
In [9]: my_package.baz
Out[9]: <module 'my_package.baz' from 'my_package/baz.pyc'>
In [10]:
Do you really want to exit ([y]/n)?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment