Why generate POP_TOP after an "import from?"

Adam Preble adam.preble at gmail.com
Fri Apr 17 14:22:01 EDT 2020


Given this in Python 3.6.8:

from dis import dis

def import_from_test():
   from sys import path

>>> dis(import_from_test)
  2           0 LOAD_CONST               1 (0)
              2 LOAD_CONST               2 (('path',))
              4 IMPORT_NAME              0 (sys)
              6 IMPORT_FROM              1 (path)
              8 STORE_FAST               0 (path)
             10 POP_TOP
             12 LOAD_CONST               0 (None)
             14 RETURN_VALUE

I don't understand why there's a POP_TOP there that I don't get for an import_name grammatical statement.

IMPORT_NAME needs to eat the top two entries of the stack for level and the from-list. BTW I don't know what level is for either since my science projects have always had it be zero, but that's another question.

IMPORT_NAME will the push the module on to the stack.

IMPORT_FROM will import path from the module on the stack, and push that result on the stack.

STORE_FAST will store path for use, finally "modifying the namespace."

At this point, my conceptual stack is empty. If I POP_TOP then I have nothing to pop and the world would end. Yet, it doesn't. What am I missing?


More information about the Python-list mailing list