why can't I declare a global variable inside an "exec" string

Ben Barrowes

I am trying to declare and change a global variable in an exec string, like this:

ostr = "didn't work"
nstr = "worked"
def function():
    exec("global ostr; ostr = nstr")
    #global ostr; ostr = nstr
    print(ostr)
    lv='ostr' in globals()
    print(lv)
    ostr='asd'

function()

However, this errors out on the print statement with:

UnboundLocalError: local variable 'ostr' referenced before assignment

But, if I comment out the "exec" line and uncomment line after the exec statement, the code works fine.

How can I fix this error using "exec"? I want to declare variables global and modify those global variables inside an exec string and have those modifications visible in "function" on subsequent lines.

Anthony1223

You have to declare that you are using global ostr in the function to be able to print it. This piece of code outputs

def function():
   global ostr
   exec("global ostr; ostr = nstr")
   #global ostr; ostr = nstr
   print(ostr)
   lv='ostr' in globals()
   print(lv)
   ostr='asd'

worked

True

Edit: Just realised the exec actually works with global variables, if you re-run your code and print(ostr) in the global main you will see it was changed.

ostr = "didn't work"
nstr = "worked"
def function():
    #global ostr
    exec("global ostr; ostr = nstr")

function()
print(ostr)

worked

Edit#2: Either declare ostr as a global variable before modifying it, or assign it to another local variable.

ostr = "didn't work"
nstr = "worked"
def function():
    exec("global ostr; ostr = nstr")
    #global ostr; ostr = nstr
    print(ostr)
    lv='ostr' in globals()
    print(lv)
        
function()

worked

True

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can I declare the datatype of a variable twice?

Why can't I define a variable inside a for loop in this way?

Why can't I set a global variable in Python?

Why can't I declare and assign global variable in a header file, even when using #ifndef HEADER_H

Why can't you declare a variable inside the expression portion of a do while loop?

Why can't I declare templated type aliases inside of functions?

Why can't I declare a variable using auto?

Why can't I initialize a variable inside a switch in Java?

Can't declare variable inside function on PostgreSQL

Can't acces a global variable inside a method

how can I declare a global variable in typescript

Why can't I use my variable inside this if statement?

Why can't I access a global variable from AsyncTask?

Why can't I declare dynamic variable inside protocol

Why can't a python string change a global variable from inside a class?

Why can't I declare a variable that takes the value of a dereferenced variable in Assembly?

Why can't I initialize the member variable inside the new?

Why can't I print a variable that is provided by user inside a loop?

Can I declare a variable inside a statement?

Why can't I use `declare -r` inside a function fo mark a variable readonly while `set -u` is in use?

Why can't I access this global variable?

I can't seem to declare this variable in java

Why can't i access the value in the global variable

Why can't I define a variable inside a definition?

Why can't I access variable inside a nested function?

I can't declare variable in MySQL Workbench

Why can't i access variable inside if loops in php

Why can't I access a global property inside the ngOnInit Method?

Why can't I access a declared variable in the global scope

TOP Ranking

HotTag

Archive