A newbie question about class

newgene newgene at bigfoot.com
Sat Feb 9 19:35:19 EST 2002


Hi, group,
     I have a newbie question about building a class:

eg.

class A:
   def __init__(self,x=None):
       self.x=x
       if self.x != None:
            self.y=x**2
       else:
            self.y=x**2

If I initialize A like "a=A(2)", then I can get "a.y=4" automatically;
but if I initialize A like "a=A()", then when I give "a.x=2" later, I
can not get the value of "a.y" like the former. Of course I can use a
method to set the value of "a.y", but is there a machanism to get the
"a.y" automatically whenever I change the value of "a.x"?


Thank you in advance.

Newgene



More information about the Python-list mailing list