Missing member

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Feb 6 16:28:09 EST 2007


Mizipzor a écrit :
> I have some troubles with a member variable that seems to be missing
> in a class. In short, heres what I do; class A is the parent class, B
> inherits from A and C inherits from B (hope I used the right words
> there). Now, I create an instance of C, which calls A's __init__ which
> in turn creates all the member variables. Then I call C.move() (a
> function defined in A), but then, one of the variables seems to have
> become 'NoneType'.
> 
> The code can be found here (Ive taken away unnecessery stuff):
> http://pastebin.com/875394
> 
> The exact error is (which occur on line 15 in the pasted code):
> TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

Alas, there's a dependency on an unknown class or function vector (which 
I presume lives in the eponym module), so we just can guess that the 
call to vector() at line 8 returned None. IOW, the problem is elsewhere...

> Any comments are welcome. :)

You ask for it, you get it:

import pygame, math
from pygame.locals import *
=> bad style
import tilemap, dataManager
from vector import *
=> idem

class _BaseEntity:

=> class _BaseEntity(object):

     def __init__(self, type, x, y):
         self._direction = vector()
         self.pos = vector(x,y)
         self.stats = dataManager.getEntityStats(type)
         self.hp = self.stats.maxHp  # todo: make all atttributes local

     def move(self):
         """ moves the entity in its direction according to its speed """
         self.pos += (self._direction * self.stats.speed)

     def setDirection(self, point, y = None):
         """ sets the direction to point, and normalises it
             if y is specifed, "point" is expected to be x,
             otherwise, "point" is expected to be a vector class """
         # make a vector
         if not y == None:
=>      if y is not None:
             point = vector(point, y)
         self._direction = point.normalise()


     #def lookAt(self, point, y = None):
     #    """ changes the angle so the entity "looks" at the specified 
coords
     #        if y is specifed, "point" is expected to be x,
     #        otherwise, "point" is expected to be a vector class """
     #    # make a vector
     #    if not y == None:
     #        point = vector(point, y)

=> code duplication, should be factored out

     #    vec = vector(point.x - self.posx, point.y - self.posy)
     #    vec.normalise()
     #
     #    angle = math.degrees(math.asin(vec.y))
     #    print angle

     def draw(self, targetSurface):
         """ blits the entire stats.image onto the targetSurface at the 
ent's coords """
         targetSurface.blit(self.stats.image, (self.pos.x,self.pos.y))

class Entity(_BaseEntity):
     def __init__(self, type, x = 0, y = 0):
         _BaseEntity.__init__(self, type, x, y)

=> You don't need to override the __init__ method if it's just to call 
the superclass's __init__ with the same args...

(snip)



More information about the Python-list mailing list