Changing global variables in embedding Python
aleax at aleax.it
Fri Sep 27 08:56:29 CEST 2002
John Dunn wrote:
> I have embedding Python in a C++ application. All is well, except I
> cannot figure out how to modify globals via Python script. After I
You talk of code inside a function, althought the snippet you show
doesn't look like it is:
> the following code, the first line works properly, but the second will
> fail with the error 'local variable 'test' referenced before
> assignment'. The message object is an argument to the function.
> message.print( test )
> test = "test2"
If a function assigns to name 'test', and does NOT have a statement
(preferably at the start), then 'test' names a LOCAL variable of
the function, not a global one. But this would make it quite
astonishing that the first line would work -- THAT line should
give the error message you're reporting for the SECOND one...
indeed the error message you report is IMPOSSIBLE for the second
line you give -- it's not referencing 'test', it's binding it.
So I suspect you mis-observed things, and the error msg is
actually for the first of these two lines. As a fix, try to
start your function with the statement:
More information about the Python-list