Help with my program

Lie Ryan lie.1296 at gmail.com
Fri Oct 23 10:26:46 CEST 2009


tanner barnes wrote:
> Ok so im in need of some help! I have a program with 2 classes and in 
> one 4 variables are created (their name, height, weight, and grade). 
> What im trying to make happen is to get the variables from the first 
> class and use them in the second class.
> 
> ------------------------------------------------------------------------
> Windows 7: It helps you do more. Explore Windows 7. 
> <http://www.microsoft.com/Windows/windows-7/default..aspx?ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen3:102009>
> 

There are many ways to do that, which one is appropriate depends on what 
you're trying to do.

1. Inheritance.

class A(object):
     def __init__(self):
         self.inst_var = 'instance variable'

class B(A)
     pass

a = A()
b = B()

2. Property

class A(object):
     def __init__(self):
         self.inst_var = 'instance variable'

class B(object):
     @property
     def inst_var(self):
         return a.inst_var
     @inst_var.setter
     def inst_var(self, val):
         a.inst_var = val

a = A()
b = B()

3. Simple copying

class A(object):
     def __init__(self):
         self.inst_var = 'instance variable'

class B(object):
     def __init__(self):
         self.inst_var = a.inst_var

a = A()
b = B()




More information about the Python-list mailing list