Question

I have some code:

# first round
curr_g = np.array([])
temp_g = np.array([1,2,3])

if curr_g is not None:
    curr_g = temp_g
    print "in is not none"
else:
    curr_g = np.c_[curr_g, temp_g]
    print "in is none"

print "curr_g: "
print curr_g

#second round
temp_g = np.array([4,5,6]) 
if curr_g is not None:
    print "in is not none"
    curr_g = temp_g
else:
    print "in none"
    curr_g = np.c_[curr_g, temp_g]

print "curr_g: "
print curr_g

And the output of the above is as follows:

in is not none
curr_g: 
[1 2 3]
in is not none
curr_g: 
[4 5 6]

Why on earth does the condition go into "Not none" both times around? curr_g is only "not None" in the second round, after being assigned temp_g.

My objective is that in the first round, because curr_g is truly empty, it should be populated with temp_g, and the second time, it should actually get concatenated to temp_g and become as follows:

[
1  4
2  5
3  6
]

How can I do this?

Was it helpful?

Solution 2

If you want to check for None, you should assign None explicitly.

curr_g = None

As BrenBarn mentioned, you have assigned an empty numpy array, and that is no more equal to None than an empty python list is.

OTHER TIPS

"Empty" is not the same as None. None is a particular object, and it is different from your (or any) numpy array, so your array is not None.

If you want to check whether your array is empty, just do if curr_g. That said, it doesn't make a lot of sense to create an empty array for curr_g if all you're going to do is overwrite it with something else. You could just initialize it with curr_g = None, and then your is None check will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top