vars between classes

Marc Tardif intmktg at CAM.ORG
Sat May 27 14:13:54 EDT 2000


Thanks, your suggested solution is exactly what I was looking for. As you
hinted, I am still confused about a few basic aspects of oop. To make sure
I understood your example, I have a couple more questions:

- Do I need to declare "class_var = 0" in first? Everything still works as
expected without it.

- Do I need to declare "self.var = var" in __init__? Couldn't I simply use
first.class_var throughout the code?

Thanks for the help,
Marc

On Sat, 27 May 2000, Doug Stanfield wrote:

> Maybe what you really want to do is the following.  It may be you're
> confusing class construction and inheritance and the effects of
> instantiation:
> 
> #!/usr/local/bin/python
> 
> class first:
> 
>     class_var = 0
> 
>     def __init__(self, var):
>          self.var = var
>          first.class_var = var
>          self.myMethod()
> 
>     def myMethod(self):
>          print "first:", self.class_var
>          second(self.var)
>          print "last:", self.class_var
> 
> class second(first):
>     def myMethod(self):
>          print "second:", self.class_var
>          first.class_var = 3
> 
> def test(var):
>     first(var)
> 
> if (__name__=='__main__'):
>     test(2)
> 
> 
> 





More information about the Python-list mailing list