how to import
Fredrik Lundh
fredrik at pythonware.com
Tue Nov 5 16:41:15 EST 2002
Fedor wrote:
> [test1.py]
> #This setup doesn't work:
> import mx.DateTime
> class A:
> def __init__(self):
> print mx.DateTime.now()
> def test(self):
> print mx.DateTime.now()
> import mx.ODBC
> a=A()
> #Results in: 2002-11-06 19:22:14.49
> a.test()
> #Results in: UnboundLocalError: local variable 'mx' referenced before
> assignment
full story:
http://www.python.org/doc/current/ref/naming.html
http://www.python.org/doc/current/ref/import.html
summary:
"import" behaves like any other assignment statement.
inside the test method, the "import mx.ODBC" statement attempts to
assign a value (the module object) to the "mx" variable.
the compiler notices this, and flags "mx" as a local variable. since "mx"
is a local variable, you cannot refer to it before it has been assigned a
value.
to fix this, either add a "global mx" statement to the method, or move
the import statement to the top of the method.
</F>
More information about the Python-list
mailing list