[Tutor] Variable Question

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 20 Nov 2000 01:20:03 -0800 (PST)


On Sun, 19 Nov 2000 FxItAL@aol.com wrote:

> Hello All,
>    I'm sure this is an easy one but I am quite perplexed over it.  In the 
> following script, how do I get the value of "optSound" out of the function to 
> be used in another module.

The easiest way to do this would be to make a global variable within your
module.  Something like:

###
optSound = ""
###

in the beginning of your program will define a global variable.  Later on
in your test() function, you can make changes to optSound:

###
def test():
    global optSound    # This line is important.  Try taking it out.
    items=list.curselection()
    items=map(int, items) 
    z=items[0]         
    optSound = "C:\\windows\\media\\" +x[z]
###

Afterwards, you should be able to access optSound from other modules.


Normally after saying this, I would recommend against global variables.  
*grin* However, since your code is short and appears to be a one-shot
thing, this is managable.  As a warning, when your program gets larger,
you'll notice that it's harder to figure out where and when your variables
change.

You'll definitely want to look into object oriented stuff when you get the
chance, because it'll let you write code that's less dependent on global
variables to maintain the state of your program.

Good luck!