[Tutor] Question about lists

Hugo Arts hugo.yoshi at gmail.com
Fri Sep 21 16:07:04 CEST 2012


On Fri, Sep 21, 2012 at 3:31 PM, Leo Degon <existentialleo at gmail.com> 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
>
> def pset(n):
>     for i in n:
>         print(i)
> class board():
>     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):
>         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()
>
>
Please always include a full traceback, it makes the error much more
obvious. For example, running your code I get:

>>>
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
()
[1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
()

Traceback (most recent call last):
  File "C:/Users/hugo/Downloads/test.py", line 35, in <module>
    x.board=x.rotate(x.board,x.size)
  File "C:/Users/hugo/Downloads/test.py", line 15, in rotate
    new=board(size)
TypeError: 'list' object is not callable
>>>

This makes it fairly obvious what is going on. in the rotate function, you
have an argument called board, and your class is also called board. You
can't have the same name refer to two different things. What happens is
that the argument "masks" the class, making it inaccessible.

I should also say it is very confusing to have a class called board with an
attribute that is also called board. You should think about naming your
variables more clearly to minimize confusion.

HTH,
Hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120921/91801f2f/attachment-0001.html>


More information about the Tutor mailing list