0

I have a method that inside it I scan an excel file in python and in another method I want to check an entry in a list in which I extracted the data from the excel sheet in the first method as follows:

def first():
    nodes_sh = xlrd.open_workbook(file_location)
    sh_method = nodes_sh.sheet_by_index(0)
    global node_data_inter
    node_data_inter = [[sh_method.cell_value(rr, co) for co in range(sh_method.ncols)] for rr in range(sh_method.nrows)] #O(rows*cols) # A loop to get all the data in the excel sheet of "Nodes Co-ordinates"
    global node_positions_ascending_inter
    node_positions_ascending_inter = dc.deepcopy(node_data_inter) #O(rows*cols)
    for rowss in range(len(node_positions_ascending_inter)): #O(rows)
        del (node_positions_ascending_inter[rowss][0:3])
        del (node_positions_ascending_inter[rowss][2])


def using_from_first_method():
    global node_positions_ascending_inter
    if node_positions_ascending_inter[0][0] == 1.25:
        print "Yes"

An error message is outputted when I type using_from_first_method()

NameError: global name 'node_positions_ascending_inter' is not defined

Why is it outputted as I already have defined node_positions_ascending_inter to be a global variable?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Mahmoud Ayman
  • 197
  • 1
  • 13
  • there's something wrong with the indents of your for loop. Since this might be related to your problem: Please fix your source code! – Marcus Müller Jul 04 '15 at 15:41
  • `node_positions_ascending_inter` is a local variable inside another function. You did not run that function nor could you access that local variable anyway. – Martijn Pieters Jul 04 '15 at 15:41
  • @MartijnPieters is right. Overlooked that one. – Marcus Müller Jul 04 '15 at 15:41
  • @MarcusMüllerꕺꕺ I've written it write in my code but here I've written it for you just to see where have I written the wrong code? – Mahmoud Ayman Jul 04 '15 at 15:42
  • @MartijnPieters Is there a way where I can access a variable from another function?? – Mahmoud Ayman Jul 04 '15 at 15:42
  • possible duplicate of [Using global variables in a function other than the one that created them](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – nalyd88 Jul 04 '15 at 15:43
  • @MahmoudAyman: that's what `return` statements are for. Return the *value* and store that value into another name where the function returns. – Martijn Pieters Jul 04 '15 at 15:44

1 Answers1

0

You need to declare node_positions_ascending_inter as a global in the using_from_first_method function. I got the below code (simplification) to run just fine.

def first():
    global node_positions_ascending_inter
    node_positions_ascending_inter = [1.25, 1.5, 1.3, 1.45]

def using_from_first_method():
    global node_positions_ascending_inter
    if node_positions_ascending_inter[0] == 1.25:
        print("Yes")

first()
using_from_first_method()

If you are still having issues maybe the problem lies in the filling of the array. Are you sure the first method is being called and successfully creating the global before the second tries to access it? Also, see the docs on globals here.

nalyd88
  • 4,940
  • 8
  • 35
  • 51
  • Or they just need to return it (provided they actually call the function) – Martijn Pieters Jul 04 '15 at 15:44
  • @MartijnPieters What if in this function I want to return another variable instead of the variable I want to make global? – Mahmoud Ayman Jul 04 '15 at 15:45
  • Maybe the code hasn't updated yet but all I see is that you declare it global in the first function. It must be declared global in both or else it is just a local variable in the second function. – nalyd88 Jul 04 '15 at 15:45
  • @MahmoudAyman: Then redesign your code not to need to do that. Generally speaking, setting globals from functions or methods should be seen as refactoring opportunities. – Martijn Pieters Jul 04 '15 at 15:48
  • @nalyd88 I've done that and it gave me the same error. – Mahmoud Ayman Jul 04 '15 at 15:48