Python Forum
Trying to understand global variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to understand global variables
#1
Just learning about using a global variable. I have this going:
x = 4

def show_value():
    print('Value of x: '+ str(x))
   # x += 3
   # print('Value of x: '+ str(x))
    
show_value()
I had to comment out those two lines in order to get the program to run. I thought that in my show_value function I could modify the value of the variable, but apparently I cannot because the compiler flagged it.

I don't quite get it. If I declare a global variable that can be seen and accessed in the show_value function, then why can't I modify it in the function as well?
Reply
#2
Okay I found out why. You have to use the global keyword to tell the interpreter that the main function intends to modify the variable.
Reply
#3
Not modify, assign.

In Python you create a variable when you do assignment. By default, the variable is created in the context where the assignment is made. If you use the global keyword, this tells Python to look in the global namespace/scope for the variable.

You do not need to use global when modifying a mutable object. For example:
x = [1]

def modify(value):
    x[0] = value

modify(5)
print(x)
Output:
5
This works because I am not assigning a value to x, I am modifying the object that was already assigned to x.
Reply
#4
(May-12-2023, 05:58 AM)deanhystad Wrote: Not modify, assign.

In Python you create a variable when you do assignment. By default, the variable is created in the context where the assignment is made. If you use the global keyword, this tells Python to look in the global namespace/scope for the variable.

You do not need to use global when modifying a mutable object. For example:
x = [1]

def modify(value):
    x[0] = value

I don't really understand your code. What are the brackets for? It reminds me of a list or an array.

modify(5)
print(x)
Output:
5
This works because I am not assigning a value to x, I am modifying the object that was already assigned to x.
Reply
#5
Why the brackets around the number?
Reply
#6
This
x = [1]
says "Make a list that contains the number 1. Assign the list to x." The square brackets tell Python to create a list.

Lists are mutable objects, meaning they are something you can change. The modify function in my example uses indexing to change the list so it contains the number 5. You can also append to lists, remove items from lists, and make slices of the list. Lists are very useful in Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Global variables or local accessible caslor 4 1,040 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,161 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 959 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Global variables not working hobbyist 9 4,753 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,672 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
  Question regarding local and global variables donmerch 12 5,121 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,454 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 3,049 Feb-09-2020, 02:05 PM
Last Post: new_to_python
  Please help me understand how to use global variables joew 6 3,603 Jan-05-2020, 06:03 PM
Last Post: joew

Forum Jump:

User Panel Messages

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