[Tutor] class accessing another's updated property

Yoram Hekma yoram.hekma at aoes.com
Thu Nov 15 12:21:07 CET 2007


On Wed, Nov 14, 2007 at 04:43:28AM -0800, ted b wrote:
> I want class One to be able to access access class
> Two's value property after its been updated. Everytime
> I try (by running, say testTwo().value) I get the
> __init__ value. The update method fro class Two is
> called elsewhere in the program, and I want class
> One's "getTwo" method to access class Two's updated
> value and give me '9' but i get '1'
> 
> Here's the code:
> 
> class One:
>    def __init__(self):
>       self.value = 3
>    def getTwo(self):
>       print "testTwo's updated value", Two().value
> 
> class Two:
>    def __init__(self):
>       self.value = 1
>    def update(self):
>       self.value = 9
> 
> Thanks in advance!

I think you want to update an instance of a class, not the class itself.
If you do:

class Two:
   def __init__(self):
      self.value = 1
   def update(self):
      self.value = 9

class One:
    def __init__(self):
        self.value = 3
        instance_of_Two = Two()
    def getTwo(self):
        print "testTwo's value", instance_of_Two().value
        instance_of_Two.update()
        print "testTwo's updated value", instance_of_Two().value

HTH
Yoram

-- 
Yoram Hekma
Unix Systems Administrator
CICT Department
AOES Netherlands B.V.
Haagse Schouwweg 6G
2332 KG Leiden, The Netherlands
Phone:  +31 (0)71 5795588
Fax:    +31 (0)71 5721277
e-mail: yoram.hekma at aoes.com
http://www.aoes.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.python.org/pipermail/tutor/attachments/20071115/0ba9e200/attachment.pgp 


More information about the Tutor mailing list