Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global not working why?
#8
That suggests that either something else is clearing the value, or you're not actually reading from the global.

Because globals can be modified anywhere in the code, debugging means looking at every assignment that the variable has throughout your code.

Also, because lists are mutable, it's possible to assign it to something else and unwittingly clear it, thinking you're clearing a copy.

def complete_form():

    global scratched_list
    scratched_list = []


    for i, var in enumerate(['a', 'b', 'c']):
        scratched_list.append(f"{i} {var}")
    return


def something_else():
    my_list = scratched_list # incorrectly assumes this is a local copy
    my_list.clear()          # clears the global as well
    return

complete_form()
print(scratched_list)
something_else()
print(scratched_list)
Output:
['0 a', '1 b', '2 c'] []
Reply


Messages In This Thread
Global not working why? - by Milfredo - Oct-02-2020, 07:29 AM
RE: Global not working why? - by ibreeden - Oct-02-2020, 08:08 AM
RE: Global not working why? - by Larz60+ - Oct-02-2020, 10:33 AM
RE: Global not working why? - by scidam - Oct-02-2020, 11:09 AM
RE: Global not working why? - by Milfredo - Oct-03-2020, 01:28 AM
RE: Global not working why? - by scidam - Oct-03-2020, 01:46 AM
RE: Global not working why? - by Milfredo - Oct-04-2020, 06:44 AM
RE: Global not working why? - by bowlofred - Oct-04-2020, 07:21 AM
RE: Global not working why? - by ndc85430 - Oct-04-2020, 10:48 AM
RE: Global not working why? - by hshivaraj - Oct-04-2020, 12:30 PM
RE: Global not working why? - by deanhystad - Oct-04-2020, 12:44 PM
RE: Global not working why? - by Milfredo - Oct-05-2020, 11:45 PM
RE: Global not working why? - by Larz60+ - Oct-06-2020, 12:29 AM
RE: Global not working why? - by Milfredo - Oct-06-2020, 02:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Global variables not working hobbyist 9 4,891 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global variable does not seem to be global. Columbo 6 3,838 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