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).
Using a `try/except NameError` wrapper is good Python. But initializing the variable to something useful before hand is equally good. I don't think there's much of difference in speed, but the code is more readable.

For example:

```
for i in range(5):
    a += 4

```

will produce a 

```
NameError: name 'a' is not defined

```

which can be trapped with:

```
for i in range(5):
    try:
        a += 4
    except NameError:
        a = np.arange(5)

```

But I could define `a` earlier. `None` is a handy value if I don't know before hand what the first value of `a` should be:

```
a=None
for i in range(5):
   if a is None:
       a=np.arange(5)
   a += 4

```

Another common `numpy` practice is to start with an empty list, and accumulate values there.

```
a = []
for i in range(5):
    a.append(np.ones(4)*i)
a = np.array(a)

```