vars between classes

Moshe Zadka moshez at math.huji.ac.il
Sat May 27 17:08:16 CEST 2000


On Sat, 27 May 2000, Marc Tardif wrote:

> OK, here's working code now. My previous code was only meant to help
> demonstrate my problem. In this new code, I have tried to change var in
> the first class by calling "first" explicitly. Unfortunately, "3" is still
> not printed:
> 
> #!/usr/local/bin/python
> 
> class first:
>   def __init__(self, var):
>     self.var = var
>     self.myMethod()
> 
>   def myMethod(self):
>     print "first:", self.var
>     second(self.var)
      ^^^^^^^^^
      Here you construct a new object of class "second", which inherits
its __init__ from "first", which calls its "myMethod", which prints
"second." It's still not very clear what you expect to happen.


>     print "last:", self.var
> 
> class second(first):
>   def myMethod(self):
>     print "second:", self.var
>     first.var = 3
> 
> def test(var):
>   first(var)
> 
> if (__name__=='__main__'):
>   test(2)
> 
> 
> 

--
Moshe Zadka <moshez at math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com





More information about the Python-list mailing list