Missing member

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mizipzor

    #1

    Missing member

    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):
    Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.


    The exact error is (which occur on line 15 in the pasted code):
    TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

    Any comments are welcome. :)

  • John Machin

    #2
    Re: Missing member

    On Feb 5, 9:45 am, "Mizipzor" <mizip...@gmail .comwrote:
    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'
    Line 15 is:
    self.pos += (self._directio n * self.stats.spee d)
    So obviously(???) self._direction is None

    What does line 8 do: self._direction = vector() ???

    I'd suggest adding line 8.1:

    assert self._direction is not None
    Any comments are welcome. :)
    I doubt that you really mean that, so I have refrained from
    commenting :-)

    Cheers,
    John

    Comment

    • Paul McGuire

      #3
      Re: Missing member

      On Feb 4, 4:45 pm, "Mizipzor" <mizip...@gmail .comwrote:
      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'
      >
      Any comments are welcome. :)
      Here's a suggestion: use new-style classes. Have _BaseEntity inherit
      from object, allows you to use super for invoking methods on super
      classes. Instead of:
      class Entity(_BaseEnt ity):
      def __init__(self, type, x = 0, y = 0):
      _BaseEntity.__i nit__(self, type, x, y)

      You enter:
      class Entity(_BaseEnt ity):
      def __init__(self, type, x = 0, y = 0):
      super(Entity,se lf).__init__(ty pe, x, y)

      This makes it easier to update your inheritance hierarchy later. New-
      style classes have other benefits too.

      As for your NoneType problem, try adding "print self._direction " to
      the end of _BaseElement.__ init__.

      -- Paul

      Comment

      • Ayaz Ahmed Khan

        #4
        Re: Missing member

        "Paul McGuire" typed:
        Here's a suggestion: use new-style classes. Have _BaseEntity inherit
        from object, allows you to use super for invoking methods on super
        classes. Instead of:
        class Entity(_BaseEnt ity):
        def __init__(self, type, x = 0, y = 0):
        _BaseEntity.__i nit__(self, type, x, y)
        >
        You enter:
        class Entity(_BaseEnt ity):
        def __init__(self, type, x = 0, y = 0):
        super(Entity,se lf).__init__(ty pe, x, y)
        >
        This makes it easier to update your inheritance hierarchy later. New-
        style classes have other benefits too.
        I am still a beginner to Python, but reading that made me think on
        impluse, "What happens in case of one class inheriting from two or more
        different classes?"

        Having written a small test case and testing it, I find that
        super().__init_ _() calls the __init__() of the first of the class in
        the list of classes from which the calling class inherits. For example:

        class C(A, B):
        def __init__(self):
        super(C, self).__init__( )

        calls A's __init__ explicity when an instance of C is instantiated. I
        might be missing something. I didn't know that.

        --
        Ayaz Ahmed Khan

        A witty saying proves nothing, but saying something pointless gets
        people's attention.

        Comment

        • Bruno Desthuilliers

          #5
          Re: Missing member

          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):
          Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

          >
          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(obj ect):

          def __init__(self, type, x, y):
          self._direction = vector()
          self.pos = vector(x,y)
          self.stats = dataManager.get EntityStats(typ e)
          self.hp = self.stats.maxH p # todo: make all atttributes local

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

          def setDirection(se lf, 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(ma th.asin(vec.y))
          # print angle

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

          class Entity(_BaseEnt ity):
          def __init__(self, type, x = 0, y = 0):
          _BaseEntity.__i nit__(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)

          Comment

          Working...