Reputation: 693
I've created a MarblesBoard class which I use to instantiate new MarblesBoard objects. I want to solve the board using the Solver class.
class MarblesBoard:
def __init__(self, marbles):
self.input = list(marbles)
print(marbles)
def switch(self):
self.input[1], self.input[0] = self.input[0], self.input[1]
print self.input
def rotate(self):
return self.input[1:] + self.input[:1]
print self.input
def is_solved(self):
if all(self.input[i] <= self.input[i+1] for i in range(len(self.input)-1)):
return True
print "True"
else:
print "Not solved!"
board = MarblesBoard((3,6,7,4,1,0,8,2,5))
board.switch()
board.rotate()
board.is_solved()
board
Works as expected.
class Solver:
def __init__(self, MarblesBoard):
print self
self.input = MarblesBoard
def solve(self):
if self.input[0] & self.input[1] != 0:
if self.input[1] < self.input[0]:
self.input.switch()
else:
self.input.rotate()
else:
self.input.rotate()
player = Solver(board)
player.solve()
Gives me AttributeError: MarblesBoard instance has no attribute 'getitem' in reference to the self.input lines in the solve function.
Upvotes: 0
Views: 56
Reputation: 77407
self.input
is an instance of the MarblesBoard
class, which contains a variable also called input
. MarblesBoard
doesn't implement a __getitem__
method to get its internal self.input
list so you have to reach into it yourself
if self.input.input[0] & self.input.input[1] != 0:
That first input
gets the MarblesBoard
instance and the second input
gets the list
inside the instance.
Suppose I implemented Solver
with a more descriptive variable name
class Solver:
def __init__(self, MarblesBoard):
print self
self.marbles_board = MarblesBoard
...
Then using it would be more self evident
def solve(self):
if self.marble_board.input[0] & self.marble_board.input[1] != 0:
...
Upvotes: 1