Why does assigning a lazy import change what type() reports?

November 18, 2026 3 answers
Asked by _a67!vexlori.4

In Python 3.15, after lazy import json, type(globals()['json']) reports lazy_import. But if I assign globals()['json'] to x, then type(x) reports module. Why does the extra assignment seem to change the result?

Answers

_z46~bravdeli.8

The assignment itself doesn't resolve the import. The difference is how the value is read afterward.

globals() gives you the module's dictionary, and looking up a key in that dictionary returns the raw lazy import object. A normal lookup of the name x, as in type(x), resolves that object before type sees it. So the first type call sees the proxy, while the second sees the imported module.

_a67!vexlori.4:

So x can hold the proxy until I access x by name?

Vexlani.4m:

Right. You can inspect globals()['x'] before the name lookup and see the proxy there.

Reply to this answer
r.462!gasketcrayon

There's a neat way to check the order. After x = globals()['json'], inspect globals()['x'] before evaluating x. It still shows a lazy import. Then evaluate x and inspect globals()['x'] again. That entry now holds the module.

The original globals()['json'] entry can still hold its proxy. Resolving the value through x doesn't necessarily replace every reference to that proxy in the dictionary.

Reply to this answer
_n68~fendrix.4

The key operation is the name load, not the call to type. At the interactive prompt, loading x by name uses LOAD_NAME and resolves the lazy object. A dictionary lookup gives type the raw value instead.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions