Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change global variables in Python [duplicate]

Tags:

python

I am trying to change a variable further down the program. I have a global variable declared at the start of the program and I want to change the variable in different functions down the program. I can do this by declaring the variable inside the function again but I would like to know is there a better way of doing this. Some test code is below to explain what I mean.

ID = 'No'
project = ("Yep"+ID) # ID added with 'No' value which I later want to change

def pro():

    ID = "YES"
    print ID

def pro1(ID):

    # I could declare project again to get this to work, but I would like to avoid this
    print project # I want this to print with the new ID number.

if __name__ == '__main__':
    pro()
    pro1(ID)

Has anyone any ideas, thanks

I have tried the global variable but when I do this the project variable still prints out YepNo instead of YepYES. I want the new variable from the function proto change the variable in the project variable.

like image 374
chrisg Avatar asked Jan 11 '10 09:01

chrisg


People also ask

Can you change global variables in Python?

In Python, global keyword allows you to modify the variable outside of the current scope.

Can you modify global variables?

Functions can access global variables and modify them. Modifying global variables in a function is considered poor programming practice. It is better to send a variable in as a parameter (or have it be returned in the 'return' statement).

Can you change a global variable in a loop?

Yes, if you decalre a variable inside a loop then that variable is called loop local variable and that variable you can not used outside the loop but if you change the global variable value then it reflect.

How do you reset global variables in Python?

You can change the value of a Python global variable by simply assigning it a new value (redeclaring). To delete a variable, use the keyword del . Trying to use a deleted variable will cause an error to fire.


3 Answers

To update global variables you could use

global ID
ID="Yes"

before assigning variable to ID = "YES"

But changing ID will be no effect on project variable, project = ("Yep"+ID), because project is already a string

you need to make a function like

def getprojectname(ID):
    return project+ID

The whole program may be like this

UPDATE: ... removed

like image 155
YOU Avatar answered Oct 14 '22 19:10

YOU


Beware, you're doing it wrong multiple times.

Even though you could use the global statement to change a global (it is discouraged since it's better to use function parameters and return values), that would NOT change other already assigned values. E.g. even though you reassign ID, you would NOT reassign project. Also: your functions return nothing, there's no point in assigning a name to their return value, and it's a BAD habit using an all-uppercase name (ID) for a variable since it's a convention to use them for constants.

This should clarify you the way global works:

myid = ''
project = ("Yep"+myid) #ID added with no value which I later want to change

def mutate_id():
    global myid
    myid = "YES"

def mutate_project():
    global project
    project = ("YEP" + myid)

if __name__ == '__main__': 
    print "myid", myid
    print "project ", project
    print

    mutate_id()

    print "myid", myid
    print "project ", project
    print

    mutate_project()

    print "myid", myid
    print "project ", project
    print

But the best way is to do WITHOUT globals:

def get_new_id(old):
    return "YES"

def get_new_project(old):
    return ("YEP" + myid)

if __name__ == '__main__': 
    myid = ''
    project = ("Yep"+myid) 

    print "myid", myid
    print "project ", project
    print

    myid = get_new_id(myid)

    print "myid", myid
    print "project ", project
    print

    project = get_new_project(project)

    print "myid", myid
    print "project ", project
    print

This will make all code interaction clear, and prevent issues related to global state change.

like image 33
Alan Franzoni Avatar answered Oct 14 '22 20:10

Alan Franzoni


Use the global statement.

The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals.

Example: http://www.rexx.com/~dkuhlman/python_101/python_101.html#SECTION004340000000000000000

P.S.

But don't use global too often, see http://www.youtube.com/watch?v=E_kZDvwofHY#t=10m45

like image 45
miku Avatar answered Oct 14 '22 18:10

miku