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-')
3
  • possible duplicate of Using global variables in a function other than the one that created them Commented Oct 2, 2013 at 14:23
  • You can also try importing __builtin__ module and add them to it: __builtin__.tm = [] Commented Oct 2, 2013 at 14:25
  • @Milo: But that would be a terrible thing to do! Commented Oct 2, 2013 at 14:26

3 Answers 3

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’.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@TylerAndFriends: "change the value" is less clear than "assigned a new value".
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:
_tkinter.TclError: no display name and no $DISPLAY environment variable
would this post help?
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.

Comments

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.