私は次のことに行き詰まります。いくつかのクラス属性をデフォルト値で初期化しようとしています。デフォルト値は可変シーケンスタイプ(リストとnumpy配列)です。クラスの外部から属性が設定されるたびにエラーチェックを行いたいので、@property機能を使用しています。
import numpy as np
from FDTimeBase import FDTimeBase
from FDString import FDString
class FDObjNetwork(FDTimeBase):
def __init__(self,i_mechObjs=[FDString(),FDString(101)],i_connPointMatrix=np.array([[0.5],[0.5]]),\
i_excPointMatrix=np.array([[0.5],[0.]])):
self._mechObjs = i_mechObjs
self._connPointMatrix = i_connPointMatrix
self._excPointMatrix = i_excPointMatrix
@property
def mechObjs(self):
"""the mechanically vibrating objects making up the network"""
return self._mechObjs
@property
def connPointMatrix(self):
"""
the connection point matrix of the network, where each row
denotes a separate element in the network and each column
denotes a separate connection
"""
return self._connPointMatrix
@property
def excPointMatrix(self):
"""
the excitation point matrix, where each row denotes a
separate element in the network and each column denotes a
separate excitation distribution
"""
return self._excPointMatrix
@mechObjs.setter
def mechObjs(self,newMechObjs):
try:
[obj.k for obj in newMechObjs]
except AttributeError:
raise AttributeError('argument mechObjs contains an invalid object')
else:
self._mechObjs = newMechObjs.tolist()[:]
@connPointMatrix.setter
def connPointMatrix(self,newConnPointMatrix):
try:
newConnPointMatrix[:,0]
except TypeError:
raise TypeError('argument connPointMatrix must be a 2D indexable object')
else:
self._connPointMatrix = np.asarray(newConnPointMatrix).copy()
@excPointMatrix.setter
def excPointMatrix(self,newExcPointMatrix):
try:
newExcPointMatrix[:,0]
except TypeError:
raise TypeError('argument excPointMatrix must be a 2D indexable object')
else:
self._excPointMatrix = np.asarray(newExcPointMatrix).cop()
# public methods
def calcModes(self):
self.__checkDimensions()
# private methods
def __checkDimensions(self):
if len(self.connPointMatrix[0,:]) != len(self.excPointMatrix[0,:]):
raise Exception('arguments connPointMatrix and excPointMatrix must contain an equal nr. of rows')
if len(self.connPointMatrix[0,:]) != len(self.mechObjs):
raise Exception('the nr. of rows of argument connPointMatrix must be equal to the nr. of\
elements in mechObjs')
if len(self.excPointMatrix[0,:]) != len(self.mechObjs):
raise Exception('the nr. of rows of argument excPointMatrix must be equal to the nr. of\
elements in mechObjs')
新しいインスタンスを作成してmechObjs属性にアクセスしようとすると、AttributeErrorが発生し、その理由がわかりません(他の2つの属性についても同じです)。インスタンスでdir()を呼び出すと、リストオブジェクトと配列オブジェクトを含む_mechObjs、_connPointMatrix、および_excPointMatrix属性があることがわかります。誰かが私がここで間違っていることを教えてもらえますか?オブジェクトFDStringは別のカスタムクラスですが、ソースファイルがかなり大きいため、投稿を省略しました。FDTimeBaseは、2つの定数クラス属性を保持するだけのオブジェクトです(特別なことは何もありません)。
前もって感謝します