https://www.dabeaz.com/courses.html
https://github.com/dabeaz-course/practical-python/blob/master/Notes/Contents.md
https://superfastpython.com/about/
https://mathspp.com/blog/building-a-python-compiler-and-interpreter
https://blog.codingconfessions.com/p/cpython-vm-internals
Explicit relative imports more better
spam/
__init__.py
foo.py
bar.py
# explicit relative imports (Better)
from . import foo # Loads ./foo.py
from .. import foo # Loads ../foo.py
from ..grok import foo # Loads ../grok/foo.py
# use an absolute module import
from spam import foo # verbose, fragile
# implicit relative imports
import foo # It works in Pyhon2, but not Python3
__init__.py
I think they should mainly be used to stitch together multiple source files into a "unified" top-level import (if desired)
# case 1
spam/
__init__.py
foo.py
bar.py
__init__.py
from .foo import Foo
from .bar import Bar
# case 2
# Each submodule should define __all__
__init__.py
from .foo import *
from .bar import *
__all__ = (foo.__all__ + bar.__all__)
Export decorator
# spam/__init__.py
__all__ = []
def export(defn):
globals()[defn.__name__] = defn
__all__.append(defn.__name__)
return defn
from . import foo
from . import bar
# spam/foo.py
from . import export
@export
def blah():
...
@export
class Foo(object):
...
__path__
定义了package的root目录,用来寻找submodule
# __init__.py
# 让package去其他目录寻找submodule
__path__.append('/some/additional/path')
sys.path、egg、zip、site.py、sys.prefix、sys.exec_prefix
# Python standard libraries usually located at
sys.prefix + '/lib/python3X.zip'
sys.prefix + '/lib/python3.X'
sys.prefix + '/lib/python3.X/plat-sysname'
sys.exec_prefix + '/lib/python3.X/lib-dynload'
# PYTHONHOME environment overrides location
# PYTHONPATH environment variable
Default arguments must appear last in definition