[Tutor] retrieving a variable from a function

Andrei Kulakov ak@silmarill.org
Wed, 14 Nov 2001 14:29:57 -0500


On Wed, Nov 14, 2001 at 04:23:59PM +0300, Cerulean Rodent wrote:
> Heya, 
> 
> Haven't posted in ages - however, today I have yet again been stopped in my tracks by a simple task. 
> Suppose I have a script that has several functions; I need to retrieve some data and store it in a 
> variable _within_ a function, and later use this data in a different function. So far all I get is a name 
> error, since the other function doesn't recognize the bloody variable - there must be a way around it, 
> but my brain cells refuse to strike me with a Coder's Satori. Can anybody help?
> 
> Cheerio, 
> 
> Pope Mickey XXIII, Patron Saint Ov Theriomorphic Memes-----------------------------------------------------

I prefer to either return the variable, or use a class.

def func1():
    var = 1
    return var

def func2(var):
    var2 = var + 2

var = func1()
func2(var)

Or with classes:

class cl:
    def __init__(self):
        self.var = 1

c = cl() # instance of class cl

def func2():
    var2 = c.var + 2



>  
>  
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org