[Tutor] How can I access a variable outside a class definition?

Andrei Kulakov ak@silmarill.org
Sat, 29 Sep 2001 15:32:32 -0400


On Sat, Sep 29, 2001 at 09:10:54PM +0200, A wrote:
> Hi,
> How can I access a variable defined in one class outside this class 
> ? For example
> 
> I have two classes like below
> 
> #############################
> class Complex:
>  def __init__(self, realpart1,imagpart1,realpart2,imagpart2):
>    self.r1 = realpart1
>    self.i1 = imagpart1
>    self.r2 = realpart2
>    self.i2 = imagpart2
>  def Add(self,r1,i1,r2,i2):
>    self.sum=self.i1+self.i2
>    ImSum=self.sum
>    return ImSum
> ###########################
> ###########################
> class Outside
>  def MyFunction(self,ImSum)
>   ...
>   ...
> 
> ########################
> 
> Is it possible to access,in the Outside class, a value of
> the ImSum
> that was return by 
> Add function in that Complex class?

Certainly, you just instantiate that class:

complex = Complex(a,b,c,d)
class Outside:
    def myfunc(self):
        var = complex.Add(e,f,g,h)

[snip]

 - Andrei


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