呼び出されていない__setitem__:__getitem__と私は次のクラス定義を持っている
class Codes():
def __new__(self, inp):
self.data = np.zeros((50,50))
self.capacity = 50
self.size = 6
self.data[:self.size,:self.size] = inp
self.x = 0
self.y = 0
return self
def __setitem__(self,coords,value):
x = coords[0]
y = coords[1]
if max(x,y) >= self.capacity:
self.capacity *= 2
newdata = np.zeroes((self.capacity,))
newdata[:self.size,:self.size] = self.data
self.data = newdata
self.data.__setitem__(coords,value)
if max(x,y) >= self.size:
print("expanding")
self.size = max(x,y)
print ("Debug")
def __getitem__(self,coords):
x = coords[0]
y = coords[1]
return self.data[x,y]
getおよびsetメソッドが呼び出されているしていないようです。私はその後、print(a[1,1])
を実行しようとすると、私はエラーを取得する
inp = np.array([[20151125,18749137,1728984,30943339,10071777,33511524],
[31916031,21629792,16929656,7726640,15514188,4041754],
[16080970,8057251,1601130,7981243,11661866,16474243],
[24592653,32451966,21345942,9380097,10600672,31527494],
[77061,17552253,28094349,6899651,9250759,31663883],
[33071741,6796745,25397450,24659492,1534922,27995004]])
a = Codes(inp)
:私は初期化しています
Traceback (most recent call last):
File "C:/Users/cotont/Dropbox/Advent of Code/Advent of Code 25-1.py", line 55, in <module>
print(a[1,1])
TypeError: 'type' object is not subscriptable
私はa[49,49] = 1
を実行しようとすると、私が手:
Traceback (most recent call last):
File "C:/Users/cotont/Dropbox/Advent of Code/Advent of Code 25-1.py", line 55, in <module>
a[49,49] = 1
TypeError: 'type' object does not support item assignment
カスタムメソッドが呼び出されないのはなぜですか、どうすれば修正できますか?
を削除
を追加し、私はスッポてるではないものをということですやりたい? 'Codes'オブジェクトはカスタムgetとsetメソッドを持つものです。 –
@TamCoton:タイプ自体ではなく、インスタンスを返したいとします。あなたのクラスは不変ではないので、空のインスタンスの作成方法を制御する必要はなく、ただ新しいインスタンスを初期化したいだけです。 '__init__'を使います。 –
ああ、完璧に機能しました。ありがとう! '__new__'の使用は、私が' 'np.ndarray''のサブクラス化を開始したためです。 –