HELP : class and variables

Bertel Lund Hansen nospamius at lundhansen.dk
Mon Sep 1 12:45:04 EDT 2003


vincent delft skrev:

>Can you explain the following ?

Yes.

>class test:
>  var1=1
>  var2=2
>  res=var1+var2

>t=test()
>print t.res
>>> 3

>t.var1=6
>print t.res
>>> 3

Changing var1 does not change res.

You might like to make it a function instead:

class Test:
  var1=1
  var2=2
  def res (self):
    return self.var1+self.var2

t=Test()
t.var1=6
print t.res()

I learned in Java to give classes names with the first letter
capitalized. I find that a useful convention.

-- 
Bertel, Denmark




More information about the Python-list mailing list