Last active
September 25, 2016 13:17
-
-
Save photonxp/be9f7ca555543dd8608c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
anypackage/ | |
__init__.py | |
xxx.py # class WWW is originally defined in this xxx file. | |
generally speaking, python can only import class from modules, which is usually a .py file. | |
import WWW from xxx | |
But, we can also import class from a package(folder) directly, rather than from the module. This is where an __init__.py file can step onto the stage. | |
anypackage/ | |
__init__.py # class WWW is then moved here. | |
xxx.py | |
After we move the class from ordinary python file xxx.py to special python file __init__.py, we can import it from "anypackage" directly: | |
import WWW from anypackage | |
Why do we need to import from package, if we can import from module? | |
When we import from a package, it can probably be viewed as a shortcut, as soft link in linux system | |
http://stackoverflow.com/questions/582723/how-to-import-classes-defined-in-init-py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment