this will be a simple question, so it won't take you much time to explain it to me, but it would help me (and possibly other people too) understand something about classes and methods and Python.
My code wants to represent a board game, at this level just on a board with N holes in a row, which can accommodate marbles numbered from 0 to N-1 (in a random order).
class MarblesBoard:
'''define the board game'''
def __init__(self, Board):
self._Board = Board
self._n = len(Board)
'''first type of move available: switch the first two marbles'''
def switch(Board):
Board[0], Board[1] = Board[1], Board[0]
return Board
'''second type of move available: move all the marbles by 1 space to the left'''
def rotate():
temp = Board[0]
for i in range (0, n-1):
Board[i] = Board[i+1]
Board[n-1] = temp
return Board
'''print the state'''
def __repr__(Board):
for i in range(0, len(Board)):
print self[i],
'''rest to be developed when this works'''
now, I test displaying the board and moving the marbles. So I let
First = MarblesBoard([1,2,3,4])
First.rotate
and only get
<repr(<instancemethod at 0x104230410>) failed: AttributeError: MarblesBoard instance has no attribute '__len__'>
I know there has already been a similar question about this error message. AttributeError: Entry instance has no attribute '__len__'
I still somehow don't see how does the same error arise in my code. Would you mind explaining (in general) when is the attribute "len" established, and maybe suggest how could it be done in this code?