Weird scoping error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Weird scoping error (/thread-15527.html) |
Weird scoping error - Stef - Jan-20-2019 I bumped into this weird scoping error and I have no clue what's going on. #module main FIXED = 0 # banks spend a fixed amount into the economy PROFIT_PERCENTAGE = 1 # banks spend a percentage of their profit into the economy CAPITAL_PERCENTAGE = 2 # banks spend a percentage of their capital into the economy SPENDING_MODES = [FIXED, PROFIT_PERCENTAGE, CAPITAL_PERCENTAGE] fixed = 5.00 # fixed amount that banks spend into the real economy, adjusted for inflation pp = 0.20 # percentage of profit that banks spend into the real economy cp = 0.20 # percentage of capital that banks spend into the real economy mr = 0.04 # minimum reserve pbdr = 0.01 # private bank payback rate nbdr = 0.05 # non bank payback rate ecbi = 0.01 # ECB interest rate pbi = 0.02 # private bank interest rate g = 0.00 # growth rate of available money. This is money that circulates in the real economy f = 0.02 # inflation im1 = [] # inside money on bank's account im2 = [] # inside money that is on non bank accounts debt = [] # outstanding debt on which interest is paid om1 = [] # outside money on ECB account om2 = [] # outside money on private banks' account bank_profit = [] # bank profit: income from interest - payoff of loans and interests to ECB bank_spending = [] # money banks spend into the real economy bank_debt = [] # outstanding debt of banks to the ECB growth = [] # growth per cycle om_growth = [] # om growth per cycle def runSimulation(initial_m2): for spending_mode in SPENDING_MODES: for i in range(500): if i == 0: initialize(initial_m2) else: # copy previous state im1.append(im1[i - 1]) im2.append(im2[i - 1]) debt.append(debt[i - 1]) om1.append(om1[i - 1]) om2.append(om2[i - 1]) bank_profit.append(0.0) bank_spending.append(0.0) bank_debt.append(bank_debt[i - 1]) # rest of code of this function is omitted def initialize(initial_im2): im1.clear() im2.clear() debt.clear() om1.clear() om2.clear() bank_profit.clear() bank_spending.clear() bank_debt.clear() growth.clear() om_growth.clear() im1.append(0.0) im2.append(initial_im2) debt.append(initial_im2) om1.append(0.0) reserve = round(initial_im2 * mr, 2) om2.append(reserve) bank_profit.append(0.0) bank_spending.append(0.0) bank_debt.append(reserve) growth.append(0.0) om_growth.append(0.0)It concerns the bank_spending variable when I first use it in the runSimulation() method. I get the following error on it: bank_spending.append(0.0) UnboundLocalError: local variable 'bank_spending' referenced before assignment I have no clue why! The variabel is defined in the global scope, just like for example bank_profit is. Why do I get this error while all the other variables don't pose a problem? Hope someone can clear this mystery for me. I'm fairly new to Python but have an extensive programming background and I know how scoping works. Must be something stupid I just don't see. Thanks in advance, Stef RE: Weird scoping error - ichabod801 - Jan-20-2019 I'm not getting any scoping problems running that code. I think the obvious solution would be not to depend on global scope. Put all the data in a dictionary and pass it explicitly as a parameter, or wrap the whole simulation code in a class. RE: Weird scoping error - perfringo - Jan-20-2019 You can read about it for example in this StackOverflow thread Long story short: give names you want modify as parameters/arguments to function EDIT: How it is explained in Python documentation RE: Weird scoping error - Stef - Jan-20-2019 Thanks for the answers. It led to actually figuring out what was going on. Somewhere in the code that I had omitted from the post I accidentally made an assignment to bank_spending instead of to bank_spending[i], as was the intention. Thanks people! Stef |