Reputation: 3397
I have code that sometimes raises an AttributeError
. The exact error is :AttributeError: 'int' object has no attribute 'place'
I create a numpy
array with a=np.eye(9, dtype=object)
then fill it with the objects as follows:
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()
When I run this code it usually stops at the last if: if places[i,j-1].place=='cave' and places[i-1,j].place=='cave':
. But it should work fine, shouldn't it?
I put the print(places)
in to check how this process runs and how it is filled. Here is the result for the first run (it has some other object of previous conditions not relevant with the problem, or maybe it is):
[[<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>]]
Here is the last one before the error where apparently everything has worked, so the code seems to work fine:
[[<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>]]
But even now, it throws the error of AttributeError: 'int' object has no attribute 'place'
. As seen above just the last row needs to be filled but in the previous iteration it behaves as if the conditions to fill the map weren't enough, when in the previous lines they were.
Any idea why this is happening, and what can I do to fix it?
Upvotes: 1
Views: 1359
Reputation: 280778
try:
places[i,j].place
except AttributeError:
if places[i,j-1].place=='cave':
places[i,j]=Place()
Do all entries of your array need to be Place
objects? For columns after the first, you only sometimes fill them with Place
s. If places[i, j-1].place
wasn't 'cave'
, you'll never fill places[i, j]
, and then on the next iteration, places[i, j-1].place
will raise an AttributeError
. This happens on the if i>=1 and j>=1:
branch, too.
Why not fill the array with Place
s at creation time, so you don't have to keep checking whether it has Place
s in it?
places = numpy.array([[Place() for i in xrange(n)] for j in xrange(n)])
Upvotes: 1