7

How to define a global array in python I want to define tm and prs as global array, and use them in two functions, how could I define them?

import numpy as np 
import matplotlib.pyplot as plt

tm = []  
prs = []

def drw_prs_tm(msg):
    tm = np.append(tm,t)
    prs = np.append(prs,s)

def print_end(msg):
    plt.plot(tm,prs,'k-')
karthikr
  • 97,368
  • 26
  • 197
  • 188
lbjx
  • 195
  • 1
  • 3
  • 12
  • 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) – John Zwinck Oct 02 '13 at 14:23
  • You can also try importing `__builtin__` module and add them to it: `__builtin__.tm = []` – Milo Oct 02 '13 at 14:25
  • @Milo: But that would be a terrible thing to do! – Eric Oct 02 '13 at 14:26

3 Answers3

16

You need to refer them as global <var_name> in the method

def drw_prs_tm(msg):
    global tm
    global prs

    tm = np.append(tm,t)
    prs = np.append(prs,s)

def print_end(msg):
    global tm
    global prs
    plt.plot(tm,prs,'k-')

Read more on global here and here

The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it as ‘global’.

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • 3
    Its important to add that you only need the `global` prefix if you want to assign a new value to the variable. If you just want to read the value, you can just use its regular name. – Tyler Oct 02 '13 at 14:26
  • 1
    @TylerAndFriends: "change the value" is less clear than "assigned a new value". – Eric Oct 02 '13 at 14:27
  • thanks!but it is really weird that I can have the array of tm and prs which both of them have the same number of element, when I use plt.plot(tm,prs,'k-'), it return with error: – lbjx Oct 03 '13 at 12:09
  • _tkinter.TclError: no display name and no $DISPLAY environment variable – lbjx Oct 03 '13 at 12:10
  • would [this post](http://stackoverflow.com/questions/13286324/mac-os-x-tkinter-tclerror-no-display-name-and-no-display-environment-variabl) help? – karthikr Oct 03 '13 at 12:11
0

With the global keyword:

def drw_prs_tm(msg):
    global tm, prs    # Make tm and prs global
    tm = np.append(tm,t)
    prs = np.append(prs,s)

Also, if you keep it as it currently is, then you do not need to declare tm and prs as global in the second function. Only the first requires it because it is modifying the global lists.

0

In case you have function inside of other function use this:

def ex8():
    ex8.var = 'foo'
    def inner():
        ex8.var = 'bar'
        print 'inside inner, ex8.var is ', ex8.var
    inner()
    print 'inside outer function, ex8.var is ', ex8.var
ex8()

inside inner, ex8.var is  bar
inside outer function, ex8.var is  bar

More: http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/

Deivydas Voroneckis
  • 1,973
  • 3
  • 19
  • 40