import dis
defimport_module():
import json
deffrom_import():
from json import dumps
deffrom_import_as():
from json import dumps as d
print("import json:")
dis.dis(import_module)
print("\nfrom json import dumps:")
dis.dis(from_import)
print("\nfrom json import dumps as d:")
dis.dis(from_import_as)
# 'import X' -> IMPORT_NAME + STORE_NAME
# 'from X import Y' -> IMPORT_NAME + IMPORT_FROM + STORE_NAME
# IMPORT_FROM is an extra step that extracts the attribute
Output
Click "Run" to execute your code
'from X import Y' compiles to IMPORT_NAME (load module) + IMPORT_FROM (extract attribute) + STORE_NAME. The module itself is not stored — only the imported name is bound.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?