[英]In Python, how can I inherit and override a method on a class instance, assigning this new version to the same name as the old one?
[英]How can I update an array by creating a new one and keeping the old one the same
我有一个 class,它的名称为字符串,您可以更改其名称(您可以在下面看到 class)但是当我使用写入function 更改正方形的名称时,它会发生一些奇怪的尝试更新它:旧数组和新数组
我还将在下面包含write_word function 作为代码
更新:我还尝试将.copy添加到write_word function,我将在更新2下面添加新版本:我还尝试将板创建为列表,并将单词写入列表。 我添加了 create 和 write 函数的数组和列表版本。 在他们两个中,问题是相同的,一旦我创建了一个板并复制它并更改副本,原始和副本都会更改。 这让我相信问题是由 class object 而不是 arrays 引起的。
Update3:似乎列表和 numpy 都有同样的问题,我现在将尝试做同样的事情,而不使用 class。 我会更新它是否有效
更新4:我根本不需要更改 class ,现在我的代码完成了它必须做的事情,这要归功于答案。 我仍将添加在 FINAL_write_word function 下工作的代码。
class Square:
def __init__(self, x,y):
self.x = x
self.y = y
self.letter = " "
self.num = 0
def write(self, letter):
if self.letter != "!":
self.letter = letter.upper()
else:
print("cannot write on black square")
def clear(self):
if self.letter != "!":
self.letter = " "
else:
print("cannot write on black square")
def black(self):
self.letter = "!"
def isblack(self):
if self.letter == "!":
return True
else:
return False
def isfilled(self):
if self.letter != "!" and self.letter != " ":
return True
else:
return False
def read(self):
return self.letter
def number(self,num):
self.num = num
def getnum(self):
return self.num
def __str__(self):
image = " " + self.letter
return image
def __repr__(self):
image = " " + self.letter
return image
def write_word(crossarray,indexes,word):
print("now writing word:",word)
temparray = crossarray
for i, index in enumerate(indexes):
row = int(index // 5)
col = int(index % 5)
temparray[row,col].write(word[i])
return temparray
def UPDATED_write_word(crossarray,indexes,word):
print("now writing word:",word)
temparray = crossarray.copy()
for i, index in enumerate(indexes):
row = int(index // 5)
col = int(index % 5)
temparray[row,col].write(word[i])
return temparray
def create_crossword(down,across,indexes,blacks):
cross_array = np.zeros((5,5), dtype=Square)
for row in range(len(cross_array)):
for col in range(len(cross_array[0])):
cross_array[row,col] = Square(row,col)
for name, index in enumerate(indexes):
row = int(index // 5)
col = int(index % 5)
cross_array[row,col].number(name+1)
for index in blacks:
row = int(index // 5)
col = int(index % 5)
cross_array[row,col].black()
return(cross_array)
def create_crosslist(down,across,indexes,blacks):
cross_list = []
for row in range(5):
rowlist = []
for col in range(5):
rowlist.append(Square(row,col))
cross_list.append(rowlist)
for name, index in enumerate(indexes):
row = int(index // 5)
col = int(index % 5)
cross_list[row][col].number(name+1)
for index in blacks:
row = int(index // 5)
col = int(index % 5)
cross_list[row][col].black()
return cross_list
def write_word_list(crosslist,indexes,word):
print("now writing word:",word)
templist = crosslist
for i, index in enumerate(indexes):
row = int(index // 5)
col = int(index % 5)
templist[row][col].write(word[i])
return templist
import copy
def FINAL_write_word(crossarray,indexes,word):
print("now writing word:",word)
temparray = copy.deepcopy(crossarray)
for i, index in enumerate(indexes):
row = int(index // 5)
col = int(index % 5)
temparray[row,col].write(word[i])
return temparray
我希望这可以帮助你:
old = np.arange(5)
new = old
new[0] = -1
上面的代码给出:
old
#array([-1, 1, 2, 3, 4])
new
#array([-1, 1, 2, 3, 4])
为避免这种情况,您必须制作副本:
old = np.arange(5)
new = old.copy()
new[0] = -1
这给了你:
old
#array([0, 1, 2, 3, 4])
new
#array([-1, 1, 2, 3, 4])
编辑
在您的特定情况下,您必须:
import copy
并更改行:
temparray = crossarray
经过:
temparray = copy.deepcopy(crossarray)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.