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...
#6
(Dec-25-2020, 12:44 AM)bowlofred Wrote:
(Dec-25-2020, 12:00 AM)ptrivino Wrote: Thank you. The thing I am trying to fix is that when I do this in a third-level function:

for curr_id in dict_1: # loop through the dictionary
# code code code
    dict_1[curr_id].incl_ips.append(ipaddress.ip_network(rng_chop[0] + "/" + str(myprefix), False))
# code code code
it appends the value in the loop iteration to THE SAME dictionary entry.

I'm not sure I follow this part. What are you expecting it to append to?

Quote:Also, from my OP, essentially dict_1_copy is discarded when I exit the function and recreated the next time the function is called?

Yes. I wouldn't use "discarded" here because the actual data remains (since it has another name outside the function). I would say it is "reassigned" the next time the function is called. No data is created at that time.

In the dict_1 entries I set the value to an object/class with 4 attributes (incl_ips is one of them), each a list of other values. But what doesn't work is that, in each loop through with a new curr_id, the SAME dictionary entry (or possibly ALL entries, I've seen that in another try)is updated/appended to.
Reply


Messages In This Thread
RE: Variable scope - "global x" didn't work... - by ptrivino - Dec-28-2020, 04:52 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,718 Nov-07-2023, 09:49 AM
Last Post: buran
  It's saying my global variable is a local variable Radical 5 1,171 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,795 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Library scope mike_zah 2 846 Feb-23-2023, 12:20 AM
Last Post: mike_zah
Question Sqlite3 how to know when cursor.execute didn't return anything ? SpongeB0B 2 865 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,505 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Scope of variable confusion Mark17 10 2,856 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,538 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,010 Feb-24-2021, 02:05 PM
Last Post: LeoT
  DataFrame operations didn't change orginal HoldYourBreath 5 2,457 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