Question: I want to know if an array is already defined somewhere before in the code.
Something like `a.exist()` gives True if it exists and False if it doesn't. 

I tried `a.size:`, but if the array doesn't exist yet, it gives an error message, which I want to avoid.

The situation demanding this happened in a loop, if you are wondering.

Answer: 
Python has some builtin-functions that can ckeck if a variable is assigned in the current/local/global scope.

For example to check if a variable is defined in the current local scopy use:

```
if 'a' in dir():
    # Variable a is defined.
    if hasattr(a, 'shape'):
        # Probably a numpy array, at least it has a shape.

```

But that's not good python, `try/except` is more common but for completness I thought it worth mentioning that you can do it without any try/except.

```
if 'a' in dir():
    print('yes')
else:
    print('no')
# prints 'no' because we haven't defined any variable a

a = np.array([1,2,3])
if 'a' in dir():
    print('yes')
else:
    print('no')
# prints 'yes' because I defined it

del a
if 'a' in dir():
    print('yes')
else:
    print('no')
# prints 'no' because I deleted the variable again

```

Also worth mentioning is [`locals`](https://docs.python.org/2/library/functions.html#locals) and [`globals`](https://docs.python.org/2/library/functions.html#globals).
In case you want to be sure that `a` is a `numpy` array and not any other type that may have a `size` variable or method, you can check the type warped in a try block to handle the exception in case the variable is not defined. This way you go ahead only if `a` exists and is a `numpy` array.

```
import numpy as np
a = np.array([1,2])
try:
    if isinstance(b,np.ndarray):
        print("TRUE")
except NameError:
    print("FALSE")

```