[Tutor] Question about lists
Dave Angel
d at davea.name
Fri Sep 21 15:53:30 CEST 2012
On 09/21/2012 09:31 AM, Leo Degon wrote:
> I'm trying to create a class where the main focus is creating a list whose
> elements are lists and the elements of those lists are collection of zeros
> and ones. I am trying to create functions to rotate the list ninety
> degrees, to reflect it. Having a few problems with the rotation.
> get TypeError: 'list' object is not callable
Where is the rest of the error message? And don't forget to mention
which Python version you're using.
> def pset(n):
> for i in n:
> print(i)
> class board():
One Python convention which would have helped here is to use capital for
the class name, Board
Another one is to put __init__ first, since that tells us what instance
attributes you'll be using, and how they're initialized.
> def make(self,size):
> b=[]
> for i in range(size[0]):
> b.append([])
> for j in range(size[1]):
> b[i].append(0)
> return b
>
> def rotate(self,board,size):
You've now defined a new local called board, which shadows the class
name board. Is there a reason you didn't use self.board ? As it's
presently coded, this method doesn't seem to use any instance attributes.
> size[0],size[1]=size[1],size[0]
> new=board(size)
> lists=[]
> for j in range(size[1]):
> lists.append([])
> for i in range(size[0]).__reversed__():
> lists[j].append(board[i][j])
> for i in range(size[1]):
> for j in range(size[0]):
> new.board[i,j]=lists[i,j]
> return(new.board)
> def __init__(self,size):
> self.size=size
> self.board=self.make(size)
> y=[7,7]
> x=board(y)
> pset(x.board)
> x.board[0][0]=1
> print()
> pset(x.board)
> print()
> x.board=x.rotate(x.board,x.size)
> pset(x.board)
> print()
>
>
--
DaveA
More information about the Tutor
mailing list