Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global vs. Local Variables
#4
Please type code in files and execute the file. This is a lot less typing and my explanation will make a lot more sense if you are used to running Python this way. If your code was in a file I think the file would look like this:
A = []

def Update(A = []):
    print("A =", A)

Update(44)
print(A)
As far as Python is concerned the code below is the same as that above. Only the names have been changed to protect the innocent
A = []

def Update(this_is_a_function_arg = []):
    print("A =", this_is_a_function_arg)

Update(44)
print(A)
The 'A' in "def Update(A=[])" is a function parameter and has no relationship at all to the "A=[]" at the top of the file. The "A" in "A=[]" is a variable that is defined in the module scope. These two things are completely different.

When you call Update(44), the value 44 is assigned to the parameter "A". Inside the function the parameter "A" is available as a variable defined within the scope of the function. You can do stuff like get the value of the variable or set the value of the variable just like any other variable. Setting the parameter variable does not change the module variable. It doesn't matter if the two have the same name or not.

Your question is about lists and scope. A list is mutable meaning you can change the list by adding or removing items. Strings and numbers are immutable. You cannot change a string, you can only create a new string which is a modified version of an existing string. It may look like the string has changed, but the old string is still around, at least until garbage collection gobbles it up and frees up the memory.

Because lists are mutable you can change the list contents without having to make a new list. This code uses the mutability of lists to allow changing the content of a list in a function and have that change be seen outside the function.
def add_square_to_global(number):
    squares.append(number**2)

def add_square_to_list(squares_list, number):
    squares_list.append(number**2)

squares = []
add_square_to_global(2)
add_square_to_list(squares, 3)
add_square_to_global(4)
add_square_to_list(squares, 5)
print(squares)
Output:
[4, 9, 16, 25]
It should also be noted here that this does not work.
def list_of_squares(squares_list):
    squares_list = []
    for a in range(1, 11):
        squares_list.append(a**2)

squares_list = None
list_of_squares(squares_list)
print(squares_list)
Output:
None
This function creates a new list instead of modifying an existing list. The list created inside the function is assigned the function parameter and does not change the "squares_list" variable outside of the function. As before, the code may as well be written like this:
def list_of_squares(some_list):
    some_list = []
    for a in range(1, 11):
        some_list.append(a**2)

squares_list = None
list_of_squares(squares_list)
print(squares_list)
Python has a "global" keyword that tells Python to create a variable outside the function scope.
def init_squares_list():
    global squares_list
    squares_list = []
    for a in range(1, 11):
        squares_list.append(a**2)

init_squares_list()
print(squares_list)
Output:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
This actually works, but as you write more Python code you'll see that this is really bad design. If you had several functions it would be difficult to see where "squares_list" was created. More importantly it is difficult to know when squares_list is created. The "squares_list" variable doesn't come into existence until the init_squares_list() function is called, and the code calling the function doesn't provide any indication that a list is created and assigned to a variable.

Instead of using "global" it is almost always better to have the function return the squares list.
Output:
def init_squares_list(): squares = [] for a in range(1, 11): squares.append(a**2) return squares squares_list = init_squares_list() print(squares_list)
This produces the same result as above, but the function call makes it obvious who is creating the squares_list.
Davy_Jones_XIV likes this post
Reply


Messages In This Thread
Global vs. Local Variables - by Davy_Jones_XIV - Jan-06-2021, 09:18 PM
RE: Global vs. Local Variables - by Larz60+ - Jan-06-2021, 09:54 PM
RE: Global vs. Local Variables - by buran - Jan-06-2021, 09:54 PM
RE: Global vs. Local Variables - by deanhystad - Jan-06-2021, 10:06 PM
RE: Global vs. Local Variables - by Davy_Jones_XIV - Jan-06-2021, 10:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,187 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Trying to understand global variables 357mag 5 1,144 May-12-2023, 04:16 PM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,808 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,043 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,163 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  How to use global value or local value sabuzaki 4 1,168 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  Clarity on global variables JonWayn 2 963 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Global variables not working hobbyist 9 4,756 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global - local variables Motorhomer14 11 4,282 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  from global space to local space Skaperen 4 2,338 Sep-08-2020, 04:59 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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