Python Forum
Global Variable Not Updating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Variable Not Updating
#1
Based on responses I got from previously posting a question about global variables, the following code should work. But it's not. Clearly, my understanding of how to use global variables in Python is still lacking. Also, I know I'll get admonished for using globals in the first place, but in this instance, I don't know of a better way to do this.

I've re-read previous replies, searched the forum and the Internet, but I still can't find an answer to this specific question. I need to track the values of certain variables not only across functions but across functions residing in modules in separate .py files within my project.

The trouble is the variable "option_ceiling" is not changing the global version of the same variable name coming out of function "take_option_loss()".

Any assistance offered is greatly appreciated.

My error message is:
Quote:Global variable 'option_ceiling' is undefined at the module level

My main module is:

import sys

import openpyxl as xl

from setup import process_first_row
from equity_processing import check_for_exit_trade, take_equity_loss, take_equity_profit, initialize_equity_variables
from option_processing import take_option_profits, check_for_option_trade, take_option_loss

option_ceiling
option_floor, 
prev_option_strike

for row in range(2, sheet.max_row):

    if sym_open_cell.value is None:  			# <-- You reached EOF so get out
        wb.save('ko_history3.xlsx')
        sys.exit()

    elif prev_exit_price == 0:
        if sym_low_cell.value < option_floor:
	    do_function_1(arg, arg)
	    do_function_2()

        elif sym_high_cell.value > option_ceiling:
	    do_function_1()

            prev_option_strike, option_strike_price, option_floor, option_ceiling = 
		take_option_loss(prev_option_strike, option_strike_price, option_strike_price_cell, 
		option_floor, option_profit_target, option_ceiling)

        else:
	    do_function_3()

    elif prev_exit_price != 0:
	do_function_4()

    else:
	error_module()

    print(f"Option Ceiling: ", option_ceiling)
The following function is stored in a .py file called "option_processing.py" within my project:

def take_option_loss(prev_option_strike, option_strike_price, option_strike_price_cell, option_floor,
                     option_profit_target, option_ceiling):

    global option_ceiling
    print("Entered: take_option_loss")
    option_ceiling += option_profit_target
    option_strike_price += option_profit_target
    option_strike_price_cell.value = option_strike_price
    prev_option_strike = option_strike_price
    option_floor += option_profit_target
    print("Exited: take_option_loss")

    return prev_option_strike, option_ceiling, option_strike_price, option_floor
The return value output from the above function is not updating the global variables in my Main Program. I'm sure this is a simple problem for a Python pro, but for a newbie like me, it's monumental. Any ideas?
Reply
#2
In main lines 28-29 you call take_option_loss and pass it option_ceiling.
In the take_option_loss function you receive option_ceiling but then declare a global option_ceiling, and you return option_ceiling. Which option_ceiling, from which namespace? I'm confused and the interpreter will be too.

If you comment out the global statement (line 4) and then make sure your return statement variable order is the same as in the calling statement (line 27 in main vs line 13 in the function) it should work. I expect that scrambling the order of the variables in the return statement ended up assigning option_ceiling the intended value for option_floor...
Reply
#3
Thanks jefsummers, that did the trick. I guess if I'm going to do goofy things with globals, I should pay closer attention to the order I'm calling and returning the variables. It works fine now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,030 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Variable scope - "global x" didn't work... ptrivino 5 2,977 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,155 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  NameError for global variable leviporton 3 2,499 Jul-10-2020, 06:51 PM
Last Post: bowlofred
  Variable Not Updating NectDz 1 2,720 Jun-07-2020, 11:21 PM
Last Post: bowlofred
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,204 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Variable not updating Rayaan 3 5,949 Mar-29-2020, 04:42 PM
Last Post: SheeppOSU
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 3,432 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  Declaring a Global Variable in a Function Bob1948 4 2,989 Sep-14-2019, 11:16 PM
Last Post: ichabod801
  Global variable does not seem to be global. Columbo 6 3,615 Jul-15-2019, 11:00 PM
Last Post: Columbo

Forum Jump:

User Panel Messages

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