2

I have the following code:

import math

class h:
    vektor = [0,0]
    rel_chyba = 0

    def __init__(self, hodnota, chyba):
        self.vektor[0] = hodnota
        self.vektor[1] = chyba
        self.rel_chyba = chyba*1.0/hodnota

    def __rmul__(self, hod2):
        return h(hod2.vektor[0]*self.vektor[0],  math.sqrt(self.rel_chyba*self.rel_chyba+hod2.rel_chyba*hod2.rel_chyba))

v = h(12,1)
print v.vektor[1]
t = h(25,2)
print v.vektor[1]

My problem is, that v.vektor[1] prints 1 for the first time and 2 for the second time. All the attributes of the object v are assigned the values of the attributes from t.

How can I create two different objects? Thanks for your answers

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tom83B
  • 2,059
  • 4
  • 18
  • 28

1 Answers1

3

Don't declare vektor at class level, that makes it a class variable. Just declare it inside __init__:

def __init__(self, hodnota, chyba):
    self.vektor = [hodnota, chyba]
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895