Python Forum
Variable scope - "global x" didn't work...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable scope - "global x" didn't work...
#1
I am struggling to understand updating a 'global' dictionary (i.e., it's defined in the main module and I want to update it in a subroutine). I understand that I need to do something since dict_1 in the mainline isn't what would be updated in a function. So I tried this experiment:

dict_1 = {}
dict_2 = {}

def upd_dict1(dict_1_copy):
    print("sub dict_1_copy a is " + str(dict_1_copy["a"]))
    dict_1_copy["a"] = 100
    print("sub dict_1_copy a is " + str(dict_1_copy["a"]))
    return dict_1_copy

if __name__ == "__main__":
    dict_1["a"] = 1
    dict_1["b"] = 2
    dict_2["a"] = 0
    dict_2["b"] = 22
    print("dict_1 a is " + str(dict_1["a"]) + " b4 upd - sb 1")
    print("dict_1 b is " + str(dict_1["b"]) + " b4 upd - sb 2")
    print("dict_2 a is " + str(dict_2["a"]) + " b4 upd - sb 0")
    print("dict_2 b is " + str(dict_2["b"]) + " b4 upd - sb 22\n")
    dict_2 = upd_dict1(dict_1)
    print("dict_1 a is " + str(dict_1["a"]) + " aft upd - sb 1")
    print("dict_1 b is " + str(dict_1["b"]) + " aft upd - sb 2")
    print("dict_2 a is " + str(dict_2["a"]) + " aft upd - sb 100")
    print("dict_2 b is " + str(dict_2["b"]) + " aft upd - sb 2\n")
    dict_1 = dict_2
    print("dict_1 a is " + str(dict_1["a"]) + " aft copy - sb 100")
    print("dict_1 b is " + str(dict_1["b"]) + " aft copy - sb 2")
    print("dict_2 a is " + str(dict_2["a"]) + " aft copy - sb 100")
    print("dict_2 b is " + str(dict_2["b"]) + " aft copy - sb 2")
Which *should* give the results shown as "sb X" in the print statements. But what I get is

Output:
dict_1 a is 1 b4 upd - sb 1 dict_1 b is 2 b4 upd - sb 2 dict_2 a is 0 b4 upd - sb 0 dict_2 b is 22 b4 upd - sb 22 sub dict_1_copy a is 1 sub dict_1_copy a is 100 dict_1 a is 100 aft upd - sb 1 <-- THIS dict_1 b is 2 aft upd - sb 2 dict_2 a is 100 aft upd - sb 100 dict_2 b is 2 aft upd - sb 2 dict_1 a is 100 aft copy - sb 100 dict_1 b is 2 aft copy - sb 2 dict_2 a is 100 aft copy - sb 100 dict_2 b is 2 aft copy - sb 2
I completely do not understand why the line marked THIS get what it gets.

Can anyone tell me what I'm missing here?

Paul
Reply


Messages In This Thread
Variable scope - "global x" didn't work... - by ptrivino - Dec-24-2020, 07:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,744 Nov-07-2023, 09:49 AM
Last Post: buran
  It's saying my global variable is a local variable Radical 5 1,181 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,801 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Library scope mike_zah 2 848 Feb-23-2023, 12:20 AM
Last Post: mike_zah
Question Sqlite3 how to know when cursor.execute didn't return anything ? SpongeB0B 2 868 Dec-18-2022, 06:13 PM
Last Post: deanhystad
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,513 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Scope of variable confusion Mark17 10 2,863 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,547 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Getting a GET request output text into a variable to work with it. LeoT 2 3,029 Feb-24-2021, 02:05 PM
Last Post: LeoT
  DataFrame operations didn't change orginal HoldYourBreath 5 2,459 Jan-10-2021, 02:01 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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