On 3/30/2018 7:25 AM, Johannes Bauer wrote:
On 30.03.2018 13:13, Ben Bacarisse wrote:

import collections

class Test(object):
        def __init__(self):
                z = {
                        "y": collections.defaultdict(list),

This mention of collections refers to ...

                }
                for (_, collections) in z.items():

... this local variable.

Yup, but why? I mean, at the point of definition of "z", the only
definition of "collections" that would be visible to the code would be
the globally imported module, would it not? How can the code know of the
local declaration that only comes *after*?

Python functions are compiled in two passes. The first pass collects all un-dotted identifiers and classifies them as global, nonlocal, or local. For CPython, the second pass does the actual compilation to bytecode. Local names are converted to indexes in a local values array.

In 3.x, 'from mod import *' is prohibited within functions to avoid the complication of needing an unoptimized backup plan.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to