1

I have this code:

latitude = pd.DataFrame()
longitude = pd.DataFrame()

datasets = []

datasets.append(latitude)
datasets.append(longitude)

def fun(key, job):
    global latitude
    global longitude

    if(key == 'LAT'):
        latitude = job.copy()
    elif(key == 'LON'):
        longitude = job.copy()

for d in datasets:
    print(d.shape)

Following this: Why does assigning to my global variables not work in Python? I have used the global keyword to ensure that the variables are correctly referenced. However, the values are not updated. Any advice? I have already verified that the if statements are correct as I am able to print something in them.

OUTPUT:

(0, 0)
(0, 0)

Moreover, I am using spyder and I can see that the list contains empty dataframe and also the global variable are empty.

2
  • how did you checked that the values are not updated?? and please add output of your values Commented Nov 30, 2017 at 9:00
  • I have edited the question, thank you. @ddor254 Commented Nov 30, 2017 at 9:05

1 Answer 1

2

o.k , the problem you have is that you make your program to point to some value in the datasets.append(latitude) and when you change the global latitude latitude = job.copy() , you changing the value of the global variable and not the one in your list datasets.

if you want to see the changed value print out latitude and you will see that the value has changed. or change the value in datasets[0] for latitude value in your loop.

Sign up to request clarification or add additional context in comments.

2 Comments

This solve the problem, however, should the variable in the list be a reference to the actual global variables? @ddor254
when changing value of the global variable you are changing what it points to. so the reference in the list still points to "old" global variable , but when you assign value to this global variable it now points to other address , which is not updated on the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.