私は次のようなPythonモジュールを作成しようとしています:
class square:
def _init_(self):
self._length = 0
self._perimeter = 0
self._area = 0
def setLength(self, length):
self._length = float(length)
self._perimeter = 0
self._area = 0
def getLength(self):
return self._length
def getPerimeter(self):
if self._perimeter == 0:
self._perimeter = float(self._length * 4)
return self._perimeter
def getArea(self):
if self._area == 0:
self._area = float(self._length * self._length)
return self._area
class rectangle:
def _init_(self):
self._length = 0
self._width = 0
self._perimeter = 0
self._area = 0
def setLength(self, length):
self._length = float(length)
self._perimeter = 0
self._area = 0
def getLength(self):
return self._length
def setWidth(self, width):
self._width = float(width)
self._perimeter = 0
self._area = 0
def getWidth(self):
return self._width
def getPerimeter(self):
if self._perimeter == 0:
perim1 = float(self._length * 2)
perim2 = float(self._width * 2)
self._perimeter = float(perim1 + perim2)
return self._perimeter
def getArea(self):
if self._area == 0:
self._area = float(self._length * self._width)
return self._area
class circle:
def _init_(self):
self._radius = 0
self._diameter = 0
self._circumference = 0
self._pi = 3.14159265
def setRadius(self, radius):
self._radius = float(radius)
self._diameter = float(self._radius * 2)
self._circumference = 0
def setDiameter(self, diameter):
self._diameter = float(diameter)
self._radius = float(self._diameter / 2)
self._circumference = 0
def getRadius(self):
return self._radius
def getDiameter(self):
return self._diameter
def getPi(self):
return self._pi
def getCircumference(self):
if self._circumference == 0:
self._circumference = float(self._diameter * self._pi)
return self._circumference
class triangle:
def _init_(self):
self._side = []
self._side[0] = 0
self._side[1] = 0
self._side[2] = 0
self._side[3] = 0
self._angle = []
self._angle[0] = 0
self._angle[1] = 0
self._angle[2] = 0
self._angle[3] = 0
self._perimeter = 0
self._area = 0
def setSide(self, side, length):
self._side[side] = float(length)
def getSide(self, side):
return self._side[side]
def getPerimeter(self):
if self._perimeter == 0:
self._perimeter = side[1] + side[2] + side[3]
return self._perimeter
def setAngle(self, angle, measure):
self._angle[angle] = float(measure)
def getAngle(self, angle):
if self._angle[angle] == 0:
if angle == 1:
angle1 = self._angle[2]
angle2 = self._angle[3]
elif angle == 2:
angle1 = self._angle[1]
angle2 = self._angle[3]
elif angle == 3:
angle1 = self._angle[1]
angle2 = self._angle[2]
anglet = angle1 + angle2
angler = float(180) - anglet
self._angle[angle] = angler
return self.angle[angle]
これは、Mathworksという名前のパッケージの一部です。呼び出し元のコードは次のとおりです。
import mathworks as mw
mycircle = mw.shapes.circle()
mycircle.setDiameter(5)
circum = mycircle.getCircumference()
print circim
2番目のモジュールを2回実行しようとすると、次のようになります。
Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "<module1>", line 21, in <module>
File "<module1>", line 17, in main
File "C:\Python27\lib\site-packages\mathworks\shapes.py", line 70, in getCircumference
self._circumference = float(self._diameter * self._pi)
AttributeError: circle instance has no attribute '_pi'
どうしたの?3.14159265に置き換えるself._pi
と機能しますが、逆に機能する必要があります。