Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local / global lists
#2
This is an odd case. You have a global "node_list", and if you left out the assignment at the end of animate_nodes, Python would use the global "node_list" and the code would run (well, eventually). The assignment changes things. The assignment creates a local variable named "node_list".
def animate_nodes():
    for current_node in node_list:  # node_list is a local variable.
        x = current_node[0] + current_node[2]
        y = current_node[1] + current_node[3]
         
        if current_node[2] > 0 and current_node[2] < 10:
            vx = current_node[2] + random.randrange(-1, 1)
             
        if current_node[3] > 0 and current_node[3] < 10:
            vx = current_node[3] + random.randrange(-1, 1)
         
        new_node_list.append = [x, y, vx, vy]
     
    node_list = new_node_list.copy()  # Assignment makes a local node_list
Use "global" to inform python you want to use the global variable
def animate_nodes():
    global node_list
    for current_node in node_list:  # node_list is a local variable.
        x = current_node[0] + current_node[2]
        y = current_node[1] + current_node[3]
         
        if current_node[2] > 0 and current_node[2] < 10:
            vx = current_node[2] + random.randrange(-1, 1)
             
        if current_node[3] > 0 and current_node[3] < 10:
            vx = current_node[3] + random.randrange(-1, 1)
         
        new_node_list.append = [x, y, vx, vy]  # This is the next bug
     
    node_list = new_node_list.copy()  # Assignment makes a local node_list
Better yet, avoid using the global variable. Pass the variable as an argument and return the modified variable. This exposes the purpose of the function instead of hiding it as a side effect.
def animate_nodes(node_list):
    """Where is my docstring?"""
    new_list=[]
    for current_node in node_list:
        x = current_node[0] + current_node[2]
        y = current_node[1] + current_node[3]
        vx = 0  # Default values?
        vy = 0  # Used but never assigned
         
        if current_node[2] > 0 and current_node[2] < 10:
            vx = current_node[2] + random.randrange(-1, 1)
             
        if current_node[3] > 0 and current_node[3] < 10:
            vx = current_node[3] + random.randrange(-1, 1)
         
        new_list.append = [x, y, vx, vy]
     
    return new_list
Reply


Messages In This Thread
local / global lists - by RedWuff - May-26-2020, 12:31 AM
RE: local / global lists - by deanhystad - May-26-2020, 03:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,185 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,805 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,040 Jan-27-2023, 05:32 PM
Last Post: caslor
  How to use global value or local value sabuzaki 4 1,167 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  Global vs. Local Variables Davy_Jones_XIV 4 2,673 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,280 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,387 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  from global space to local space Skaperen 4 2,335 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Question regarding local and global variables donmerch 12 5,124 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,455 Apr-08-2020, 06: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