Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weird scoping error
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked Script get's really weird error DaJohn 7 2,555 May-26-2024, 09:10 AM
Last Post: snippsat
  Weird function defaults error? wallgraffiti 5 4,036 Aug-07-2020, 05:55 PM
Last Post: deanhystad
  Weird SQLAchemy connection string error pawpaw 0 2,390 Jun-28-2020, 10:11 AM
Last Post: pawpaw
  [split] Python beginner: Weird Syntax Error mnsaathvika 1 3,216 Jul-22-2019, 06:14 AM
Last Post: buran
  Understanding Scoping in Python yksingh1097 5 5,933 Aug-06-2018, 07:42 PM
Last Post: nilamo
  Scoping Question: If else in for loop not evaluating i as expected datasundae 3 4,632 May-25-2018, 06:43 PM
Last Post: datasundae
  Weird error in pycharm TheRedFedora 1 3,799 Mar-11-2018, 09:01 PM
Last Post: Larz60+
  weird error in random sentence generator bobger 9 8,255 Nov-29-2017, 07:34 PM
Last Post: bobger
  Error Handling is weird PythonAndArduino 1 3,947 Nov-09-2017, 05:08 AM
Last Post: Mekire
  Python beginner: Weird Syntax Error mentoly 5 13,337 Oct-13-2017, 08:06 AM
Last Post: gruntfutuk

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.