Python Forum
Please help me understand how to use global variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help me understand how to use global variables
#1
I'm new to Python as of 1 week ago and I'm trying to write a simple stock market backtester. In my program, I want to change a global variable called 'prev_entry_price' within a function I've defined and saved in a .py file within my project. But the variable doesn't change outside the function, and I don't understand why.

# -----------------
# Here is my code:
# -----------------

prev_entry_price = 0
prev_exit_price = 0

for row in range(2, 5):
    # for row in range(2, sheet.max_row + 1):
    sym_date_cell = sheet.cell(row, 1)
    sym_open_cell = sheet.cell(row, 2)
    sym_high_cell = sheet.cell(row, 3)
    sym_low_cell = sheet.cell(row, 4)
    sym_close_cell = sheet.cell(row, 5)
    sym_volume_cell = sheet.cell(row, 7)
    entry_price_cell = sheet.cell(row, 12)
    exit_price_cell = sheet.cell(row, 13)

    setup_eoddata(row, sym_open_cell, entry_price_cell, prev_entry_price, exit_price_cell, prev_exit_price)

print(f"Row ", row, prev_entry_price, entry_price_cell.value, exit_price_cell.value)


def setup_eoddata(row, sym_open_cell, entry_price_cell, exit_price_cell, prev_exit_price):
    if row == 2:
        global prev_entry_price
        entry_price = sym_open_cell.value
        entry_price_cell.value = entry_price
        prev_entry_price = entry_price
        exit_price = 0
        exit_price_cell.value = exit_price
        prev_exit_price = exit_price

        return prev_entry_price, prev_exit_price
# --------------------------
# My input looks like this:
# --------------------------

1/2/2019	46.94	47.22	46.56	46.93	45.83	11,603,700
1/3/2019	46.82	47.37	46.53	46.64	45.55	14,714,400
1/4/2019	46.75	47.57	46.64	47.57	46.45	13,013,700
1/7/2019	47.57	47.75	46.90	46.95	45.85	13,135,500
# --------------------------
# My output looks like this:
# --------------------------

Output:
Row 2 0 46.939999 0 Row 3 0 None None Row 4 0 None None
Process finished with exit code 0

The third column on the third row should contain '46.939999' but instead, it always comes back as '0'. When I use the debugger, it shows the modified value within the function, as expected. It even shows the modified value in both my calling argument, and called parameter. But still, I get '0' when I print. I've read and tried everything I could find on the Internet about how to use global variables to do this, and I can't get it to work. One other thing, the parameter on my function is grayed out, and when I hover my cursor over it, I see "Parameter 'prev_entry_price' value is not used." Does this matter?

Any help I receive would be greatly appreciated.
Reply
#2
First of all, the correct way to use global variables is to not use global variables. Use return statements and assignments:

def foo():
    x = 801
    return x

y = foo()
Second, you function only does anything if row is 2, because of the conditional on line 21. The return statement for the function is part of that conditional, so if row != 2, the default return value of None is returned, which is why your output is full of None's.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you for your response, ichabod801. I understand about the conditional 'If' statement. That was an oversight on my part. I added an 'elif' and 'else', then I moved the return statement to the end of the function and I still get the same thing:


# -----------------
# Here is my code:
# -----------------
prev_entry_price = 0
prev_exit_price = 0

for row in range(2, 5):
    # for row in range(2, sheet.max_row + 1):
    sym_date_cell = sheet.cell(row, 1)
    sym_open_cell = sheet.cell(row, 2)
    sym_high_cell = sheet.cell(row, 3)
    sym_low_cell = sheet.cell(row, 4)
    sym_close_cell = sheet.cell(row, 5)
    sym_volume_cell = sheet.cell(row, 7)
    entry_price_cell = sheet.cell(row, 12)
    exit_price_cell = sheet.cell(row, 13)

    setup_eoddata(row, sym_open_cell, entry_price_cell, prev_entry_price, exit_price_cell, prev_exit_price)

print(f"Row ", row, prev_entry_price, entry_price_cell.value, exit_price_cell.value)


def setup_eoddata(row, sym_open_cell, entry_price_cell, exit_price_cell, prev_exit_price):
    if row == 2:
        global prev_entry_price
        entry_price = sym_open_cell.value
        entry_price_cell.value = entry_price
        prev_entry_price = entry_price
        exit_price = 0
        exit_price_cell.value = exit_price
        prev_exit_price = exit_price

    elif prev_exit_price == 0:
        entry_price_cell.value = prev_entry_price
        exit_price_cell.value = prev_exit_price
    else:
	    Print("Logic Error!")

    return prev_entry_price, prev_exit_price
# --------------------------
# My output looks like this:
# --------------------------
Output:
Row 2 0 46.939999 0 Row 3 0 0 0 Row 4 0 0 0 Process finished with exit code 0
# --------------------------------
# My output should look like this:
# --------------------------------
Output:
Row 2 0 46.939999 0 Row 3 46.939999 46.939999 0 Row 4 46.939999 46.939999 0 Process finished with exit code 0
The first and second columns on the third and fourth rows should contain '46.939999' but instead it always comes back as '0'. When I use the debugger, it shows the modified value within the function, as expected. It even shows the modified value in both my calling argument, and called parameter. But still, I get '0' when I print.

Any help I receive would be greatly appreciated.
Reply
#4
And that's a great example of why not to use global. Your global statement is still in the conditional, so the variable is only global if row is 2.

Here's how to fix it. First, delete line 22. Then change line 15 to:

prev_entry_price, prev_exit_price = setup_eoddata(row, sym_open_cell, entry_price_cell, prev_entry_price, exit_price_cell, prev_exit_price)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks again for responding ichabod801. And thank you for doctoring my post. I've read the instructions in the link you sent me and I'll do it properly in the future.

Meanwhile, your recommendations fixed my code alright. Thank you very much! I'm getting exactly what I want now. The reason I was trying to use a global is that I need all 'prev_' values to persist through the processing of the next record in the row.

If I understand how to use the 'return' statement correctly as you've shown it, I don't need a global because the function values are returned to the calling statement after each iteration, thus accomplishing the same thing.

Is my understanding correct?
Reply
#6
Well, I don't understand line 17. Your output makes it seem that line 17 is printing each time through the loop. But in what you've posted, it's not indented under the loop, so it should only be printing once.

If that is a typo in what you posted, and it actually is indented under the loop, then yes: the function is returning the value each time through the loop, giving you what you need.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Yes, that's a typo. It prints each time through the loop. I don't know if I can edit that code block to correct the mistake for future readers of this thread. It's only there for debugging purposes and will be removed in the final program. Thanks again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,065 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 985 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,102 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 905 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Global variables not working hobbyist 9 4,619 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,597 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,128 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Question regarding local and global variables donmerch 12 4,978 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,359 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 2,886 Feb-09-2020, 02:05 PM
Last Post: new_to_python

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020