Python Forum
How to change global variable in function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change global variable in function?
#1
Hello, I need your help, I have to make program containing changing a global variable using a function. But, without using "global". Do you have any idea how can I get around this?

Thanks in advance.
Reply
#2
your function should return value and you will assign the returned value to the variable you want to change. The function may or may not take arguments.

def multiply(number):
    return number*3

my_var = 2
my_var = multiply(my_var)
print(my_var)
in this example I pass my_var as argument to the function, but that's not mandatory nor always the case
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I'm confused. Do you need to change a global variable to the result of a function? Then you would just return the value from the function, and assign that to the global variable:

SPAM = 1

def eggs():
    return 2

SPAM = eggs()
Or is your assignment to so the change to the global variable within the function without using the global statement?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Thank you very much. :) All I need to do with this is a global variable type list and a function, which gets data and then save list into this variable. So, when I understand that correctly, all I have to do is creating a local variable type list inside a function, get data to this list, and this local variable make as return of that function and assign it to the global one, am I right?
Reply
#5
that would be the better approach.
However you should know that list is a mutable type, and you can change the list elements inside the function without using global
my_list = [1, 2]
def change_list():
    my_list[0] = 3
    my_list[1] = 4

change_list()
print(my_list)

def change_list1(some_list):
    some_list[0] = 10
    some_list[1] = 11

change_list1(my_list)
print(my_list)

def change_list2():
    return [5, 6]

my_list = change_list2()
print(my_list)
Output:
[3, 4] [10, 11] [5, 6]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
@buran that´s a good reminder, thanks for that. I have not realised this before.

Thanks for detailed explanation to both of you. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 548 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 516 Nov-23-2023, 02:53 PM
Last Post: rob101
  It's saying my global variable is a local variable Radical 5 1,098 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Printing the variable from defined function jws 7 1,166 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 892 Aug-07-2023, 05:58 PM
Last Post: Karp
  Retrieve variable from function labgoggles 2 1,000 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,836 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  Function global not readable by 'main' fmr300 1 1,297 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  write new function or change the old one to work "smartter? korenron 3 1,926 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  how can a variable change if I haven't changed it? niminim 5 2,992 Apr-07-2021, 06:57 PM
Last Post: niminim

Forum Jump:

User Panel Messages

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