Python Forum
*** NameError: name '' is not defined - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: *** NameError: name '' is not defined (/thread-17354.html)



*** NameError: name '' is not defined - adamG - Apr-08-2019

I want to compare the first column of an array with all the other columns and then break the function if they are all the same.

Outside the function, this code works.

import numpy as np
comms = np.array([[3, 3, 2, 2],
       [3, 2, 0, 3],
       [2, 2, 0, 4],
       [0, 4, 0, 4],
       [1, 0, 0, 2]])

M = comms.shape[0]
all((comms[i, : ] == comms[0, : ]).all() for i in range(M))
However, evaluated inside the function, it fails

def varinfo(comms):
    M = comms.shape[0]   
    
    return all((comms[i, : ] == comms[0, : ]).all() for i in range(M))
This is very strange. Could someone help?


RE: *** NameError: name '' is not defined - ichabod801 - Apr-08-2019

I'm not getting an error running either batch of code. For the second one, you are defining comms and passing it to varinfo as a parameter, right? This is how I'm running the second batch of code:

import numpy as np
comms = np.array([[3, 3, 2, 2],
       [3, 2, 0, 3],
       [2, 2, 0, 4],
       [0, 4, 0, 4],
       [1, 0, 0, 2]])

def varinfo(comms):
    M = comms.shape[0]

    return all((comms[i, : ] == comms[0, : ]).all() for i in range(M))

varinfo(comms)