Weird import behavior

Fredrik Lundh fredrik at pythonware.com
Wed Oct 26 03:40:06 EDT 2005


"Tommytrojan" wrote:

> thanks for your quick reply. I guess I should have included the output.
> I thought I was clear in the error description.
> The problem is that I never assign to 'string'. I only reference it (as
> the error message correctly states). If you comment out the import
> statement in the except clause the program runs fine. Now notice that
> the exception hander never gets executed! Any explanation?

the language reference has the full story:

    http://docs.python.org/ref/naming.html

    "If a name is bound in a block, it is a local variable of that
    block." /.../

    "The following constructs bind names: formal parameters
    to functions, import statements, class and function definitions
    (these bind the class or function name in the defining block),
    and targets that are identifiers if occurring in an assignment,
    for loop header, or in the second position of an except
    clause header. The import statement of the form "from ...
    import *" binds all names defined in the imported module,
    except those beginning with an underscore. /.../

    A target occurring in a del statement is also considered bound
    for this purpose (though the actual semantics are to unbind
    the name). /.../

    If a name binding operation occurs anywhere within a code block,
    all uses of the name within the block are treated as references to
    the current block."

</F>






More information about the Python-list mailing list