У меня есть код, который иногда вызывает AttributeError
. Точная ошибка: AttributeError: 'int' object has no attribute 'place'
создать numpy
массив с a=np.eye(9, dtype=object)
затем заполнить его с объектами следующим образом:Заполнить массив numpy объектами
for i in range(9): #rows
#Create the initial Place for each row in the column 0
j=0
#Check if there is something already stored if not store a new object
try:
places[i,j].place #An attribute of the
except:
places[i,j]=Place() #Where Place() is the object (it has the attribute place)
for j in range(9): #columns
#For the other columns of the first row(i==0) create a Place according to the previous Place
if i==0 and j>=1:
#If there is something already don't create a new Place
try:
places[i,j].place
except AttributeError:
if places[i,j-1].place=='cave':
places[i,j]=Place()
if i>=1 and j>=1:
try:
places[i,j].place
except:
print(places)
if places[i,j-1].place=='cave' and places[i-1,j].place=='cave':
places[i,j]=Place()
Когда я запускаю этот код обычно останавливается на последней, если: if places[i,j-1].place=='cave' and places[i-1,j].place=='cave':
. Но это должно работать нормально, не так ли?
Я положил print(places)
, чтобы проверить, как этот процесс работает и как он заполняется. Вот результат для первого запуска (он имеет какой-то другой объект предыдущих условий, не соответствующий с проблемой, или, может быть, оно и есть):
[[<probe_2.Place object at 0x03920690> <probe_2.Place object at 0x038C4F70>
<probe_2.Place object at 0x03912B70> <probe_2.Place object at 0x039083F0>
<probe_2.Place object at 0x038C4F90> <probe_2.Place object at 0x03912AD0>
<probe_2.Place object at 0x038D1790> <probe_2.Place object at 0x03846AB0>
<probe_2.Place object at 0x038DF630>]
[<probe_2.Place object at 0x03920330> <probe_2.Place object at 0x03912C30>
<probe_2.Place object at 0x03912BF0> 0 0 0 0 0 0]
[0 <probe_2.Place object at 0x03912C10>
<probe_2.Place object at 0x03912BD0> 0 0 0 0 0 0]
[0 0 <probe_2.Place object at 0x03912C50> 1 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 <probe_2.Place object at 0x03912B90>
<probe_2.Place object at 0x03912BB0> 0 0]
[0 0 0 0 0 0 1 <probe_2.Place object at 0x03912ED0> 0]
[0 0 0 0 0 0 0 <probe_2.Place object at 0x03912C70>
<probe_2.Place object at 0x03912CB0>]
[0 0 0 0 0 0 0 <probe_2.Place object at 0x03912C90>
<probe_2.Place object at 0x03912CD0>]]
Вот последний перед ошибкой, где, видимо, все работает, поэтому код, кажется, работает нормально:
[[<probe_2.Place object at 0x036EF6B0> <probe_2.Place object at 0x036D7FF0>
<probe_2.Place object at 0x03660410> <probe_2.Place object at 0x03656AB0>
<probe_2.Place object at 0x036D7470> <probe_2.Place object at 0x036E1AF0>
<probe_2.Place object at 0x03656AB0> <probe_2.Place object at 0x03660410>
<probe_2.Place object at 0x0372F910>]
[<probe_2.Place object at 0x036EF350> <probe_2.Place object at 0x036E1C50>
<probe_2.Place object at 0x036E1C10> <probe_2.Place object at 0x03660670>
<probe_2.Place object at 0x03721D70> <probe_2.Place object at 0x036E1B50>
<probe_2.Place object at 0x036E1B50> <probe_2.Place object at 0x03660470>
<probe_2.Place object at 0x03721D70>]
[<probe_2.Place object at 0x03656A90> <probe_2.Place object at 0x036E1C30>
<probe_2.Place object at 0x036E1BF0> <probe_2.Place object at 0x036E1B90>
<probe_2.Place object at 0x036D7FF0> <probe_2.Place object at 0x03721D70>
<probe_2.Place object at 0x03656AB0> <probe_2.Place object at 0x03656AF0>
<probe_2.Place object at 0x0372F550>]
[<probe_2.Place object at 0x03656A70> <probe_2.Place object at 0x03660410>
<probe_2.Place object at 0x036E1C70> <probe_2.Place object at 0x0372F690>
<probe_2.Place object at 0x0372F910> <probe_2.Place object at 0x036E1B90>
<probe_2.Place object at 0x03656AF0> <probe_2.Place object at 0x036D7FF0>
<probe_2.Place object at 0x03660410>]
[<probe_2.Place object at 0x036EF0F0> <probe_2.Place object at 0x03656AF0>
<probe_2.Place object at 0x03721D70> <probe_2.Place object at 0x036D7470>
<probe_2.Place object at 0x036D7470> <probe_2.Place object at 0x036E1B70>
<probe_2.Place object at 0x03721D70> <probe_2.Place object at 0x03660410>
<probe_2.Place object at 0x036E1B70>]
[<probe_2.Place object at 0x036EF290> <probe_2.Place object at 0x03660670>
<probe_2.Place object at 0x036D7470> <probe_2.Place object at 0x03721D70>
<probe_2.Place object at 0x0372F690> <probe_2.Place object at 0x036E1BB0>
<probe_2.Place object at 0x036E1BD0> <probe_2.Place object at 0x036E1B50>
<probe_2.Place object at 0x03656AF0>]
[<probe_2.Place object at 0x036EF170> <probe_2.Place object at 0x03721D70>
<probe_2.Place object at 0x036E1B50> <probe_2.Place object at 0x036D7470>
<probe_2.Place object at 0x0372F550> <probe_2.Place object at 0x0372F690>
<probe_2.Place object at 0x0372F910> <probe_2.Place object at 0x036E1EF0>
<probe_2.Place object at 0x0372F910>]
[<probe_2.Place object at 0x036EF650> <probe_2.Place object at 0x03656AF0>
<probe_2.Place object at 0x03656AB0> <probe_2.Place object at 0x0372F690>
<probe_2.Place object at 0x03656AF0> <probe_2.Place object at 0x0372F550>
<probe_2.Place object at 0x0372F910> <probe_2.Place object at 0x036E1C90>
<probe_2.Place object at 0x036E1CD0>]
[<probe_2.Place object at 0x036EF130> <probe_2.Place object at 0x03656AB0>
0 0 0 0 0 <probe_2.Place object at 0x036E1CB0>
<probe_2.Place object at 0x036E1CF0>]]
Но даже сейчас, он бросает ошибку AttributeError: 'int' object has no attribute 'place'
. Как видно выше, только последняя строка должна быть заполнена, но в предыдущей итерации она ведет себя так, как если бы условия заполнения карты были недостаточными, когда в предыдущих строках они были.
Любая идея, почему это происходит, и что я могу сделать, чтобы исправить это?
10 Вы говорите, что вы создаете матрицу идентификации 4x4, но затем вы пытаетесь пройти через 9 строк и столбцов. Что с этим? – user2357112
Извините, моя ошибка. Я создаю массив 9x9, я отредактировал вопрос – Llopis