Why generate POP_TOP after an "import from?"

Adam Preble adam.preble at gmail.com
Fri Apr 17 14:31:38 EDT 2020


On Friday, April 17, 2020 at 1:22:18 PM UTC-5, Adam Preble wrote:

> 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?

Check out this guy replying to himself 10 minutes later.

I guess IMPORT_FROM pushes the module back on to the stack afterwards so that multiple import-from's can be executed off of it. This is then terminated with a POP_TOP:

>>> def import_from_multi():
...     from sys import path, bar
...
>>> dis(import_from_multi)
  2           0 LOAD_CONST               1 (0)
              2 LOAD_CONST               2 (('path', 'bar'))
              4 IMPORT_NAME              0 (sys)
              6 IMPORT_FROM              1 (path)
              8 STORE_FAST               0 (path)
             10 IMPORT_FROM              2 (bar)
             12 STORE_FAST               1 (bar)
             14 POP_TOP
             16 LOAD_CONST               0 (None)
             18 RETURN_VALUE


More information about the Python-list mailing list