something wrong with isinstance

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Feb 13 09:50:15 CET 2009


maksym.kaban at gmail.com a écrit :
> Hi there.
> now i'm a complete newbie for python, and maybe my problem is stupid
> but i cannot solve it myself
> 

Others already addressed your problem (cf Paul and Diez answers). I'll 
just allow myself to point a couple other potential problems with your code:

> ##worldmap module
> class GeoMap:
>     cells = []
>     activerow = 0
>     activecol = 0


Attributes defined at the class level are *class* attributes, shared by 
all instances of the class - that is, all instances will access the one 
same 'cells' list. Instance attributes are canonically created in the 
initialize method (__init__) that is automagically called on 
instanciation. IOW, you want to replace the above lines with:

      def __init__(self):
          self.cells = []
          self.activerow = 0
          self.activecol = 0


>     def addCell(self, acell):
>         if len(self.cells) == 0:

An empty list evals to False in a boolean context, so the above can be 
simply expressed as:
           if not self.cells:

>           self.cells.append([])
>           self.activerow = 0
>         acell.col = self.activerow
>         acell.row = self.activecol
>         self.cells[self.activerow].append(acell)
>         self.activecol += 1
> 
>     def addRow(self):
>         self.cells.append([])
>         self.activerow += 1;
>         self.activecol = 0;
> 
> class GeoMapCell:
>     neighbours = (None, None, None, None, None, None, )
>     col = 0
>     row = 0

Same remark as above. You want to move all this code to the __init__ method.




More information about the Python-list mailing list