vars between classes

Moshe Zadka moshez at math.huji.ac.il
Sat May 27 10:56:31 EDT 2000


On Sat, 27 May 2000, Marc Tardif wrote:

> How can I maintain variables between classes and superclasses without
> passing explicitly as methods arguments? For instance:
> 
> class first:
>   def __init__(self, var):
>     self.var = var
>     self.myMethod()
> 
>   def myMethod():
>     print "first:", self.var
>     second(self.var)
>     print "last:", self.var
> 
> class second(first):
>   def myMethod():
>     print "second:", self.var
>     self.var = 3
> 
> def test(var):
>   first(var)
> 
> if (__name__=='__main__'):
>   test(2)

Your Python code doesn't work:

Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in test
  File "<stdin>", line 4, in __init__
TypeError: no arguments expected

In case you're trying to call methods of superclasses in subclasses, here
it is:

class foo:

	def method(self):
		print "foo"

class bar(foo):

	def method(self):
		print "bar"
		foo.method(self) # explicitly call foo's "method"

a = bar()
a.method

(Prints

bar
foo)






More information about the Python-list mailing list