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.

Guido Muscioni
  • 1,203
  • 3
  • 15
  • 37

1 Answers1

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.

ddor254
  • 1,570
  • 1
  • 12
  • 28
  • This solve the problem, however, should the variable in the list be a reference to the actual global variables? @ddor254 – Guido Muscioni Nov 30 '17 at 09:12
  • 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. – ddor254 Nov 30 '17 at 09:14