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
  write new function or change the old one to work "smartter? korenron 3 248 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  how can a variable change if I haven't changed it? niminim 5 775 Apr-07-2021, 06:57 PM
Last Post: niminim
  Variable scope - "global x" didn't work... ptrivino 5 795 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Finding global extrema of oscillating function JoeRogan 0 489 Dec-22-2020, 01:49 AM
Last Post: JoeRogan
  How to make global list inside function CHANKC 6 924 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Change variable value during a while loop? penahuse 2 1,058 Nov-15-2020, 11:53 PM
Last Post: penahuse
  Change variable in an outside file ebolisa 5 664 Nov-11-2020, 04:41 AM
Last Post: ebolisa
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 708 Nov-07-2020, 08:59 AM
Last Post: buran
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 532 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  passing variable to function Rejoice 4 1,114 Sep-11-2020, 03:27 AM
Last Post: Pleiades

Forum Jump:

User Panel Messages

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