我有以下类定义:
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方法。我正在初始化:
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)
如果我尝试执行print(a[1,1])
,我会收到错误:
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
为什么我的自定义方法没有被调用,我该如何解决?
答案 0 :(得分:6)
您误解__new__
在您应该使用__init__
的地方做了什么或意外使用了什么。您将返回Codes
返回self
对象本身:
def __new__(self, inp):
# ...
return self
__new__
在类上被称为静态方法,并且应该在方法中生成一个新的实例。
因为您返回了类型对象(类),所以在__getitem__
(默认元类)(请参阅Special method lookup)上查找__setitem__
和type
,
你可能想在那里使用__init__
;在已创建的实例上调用__init__
:
class Codes():
def __init__(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
__init__
不需要返回任何内容(无论如何都会忽略它返回的内容),因此您可以完全删除return self
行。
如果您认为必须使用__new__
(可能是因为您是子类化不可变类型),那么至少要创建一个super().__new__()
的实例:
class Codes():
def __new__(cls, inp):
instance = super(Codes, cls).__new__(cls)
instance.data = np.zeros((50,50))
instance.capacity = 50
instance.size = 6
instance.data[:self.instance,:instance.size] = inp
instance.x = 0
instance.y = 0
return instance
但使用__init__
只是更简单。
答案 1 :(得分:1)
添加
import numpy as np
重命名
__new__(self, inp):
....
到
__init__(self, inp):
....
并删除行
return self