Python Forum
Variables being overridden to initial values.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variables being overridden to initial values.
#2
because they have local scope inside the function. you need to declare them as global, using global keyword if you want to change the global name.
Note, if you don't assign to a name inside the function, it will use the global one, without need to declare it global
e.g.
def SET_MOST_RECENT_BACKUP():
    print("Step 3...")
    global MOST_RECENT_BACKUP
    MOST_RECENT_BACKUP = CURRENT_LIST_OF_BACKUPS[-1]
    print(MOST_RECENT_BACKUP)
CURRENT_LIST_OF_BACKUPS is a list and lists are mutable, that's why it "works" for it.

Note that using globals is generally considered bad, pass arguments to functions and return from functions.
Also, using ALL_CAPS for function and names is not consistent with PEP8 recommendations.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Variables being overridden to initial values. - by buran - Oct-06-2020, 05:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Changing the initial worksheet name in an MS Excel file azizrasul 3 1,057 Oct-02-2022, 07:56 PM
Last Post: azizrasul
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,594 Jul-27-2022, 08:50 PM
Last Post: rob101
  How to move multiple columns to initial position SriRajesh 4 1,489 Jul-02-2022, 10:34 AM
Last Post: deanhystad
  Create array of values from 2 variables paulo79 1 1,177 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 2,019 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Giving all possible values to four different variables quest_ 7 3,117 Jan-18-2021, 05:18 AM
Last Post: deanhystad
  Print variable values from a list of variables xnightwingx 3 2,729 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Assign dynamic values to Variables srikanthpython 6 3,560 Jun-06-2020, 03:36 PM
Last Post: srikanthpython
  Differential equations with initial condition in Python (change a working code) Euler2 1 1,900 May-29-2020, 04:06 PM
Last Post: Euler2
  Help with initial conditions Dantooine 1 1,409 Oct-18-2019, 07:22 PM
Last Post: UGuntupalli

Forum Jump:

User Panel Messages

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