By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

Missing member

P: n/a
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. :)

Feb 4 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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._direction * self.stats.speed)
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

Feb 4 '07 #2

P: n/a
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(_BaseEntity):
def __init__(self, type, x = 0, y = 0):
_BaseEntity.__init__(self, type, x, y)

You enter:
class Entity(_BaseEntity):
def __init__(self, type, x = 0, y = 0):
super(Entity,self).__init__(type, 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

Feb 5 '07 #3

P: n/a
"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(_BaseEntity):
def __init__(self, type, x = 0, y = 0):
_BaseEntity.__init__(self, type, x, y)

You enter:
class Entity(_BaseEntity):
def __init__(self, type, x = 0, y = 0):
super(Entity,self).__init__(type, 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.

Feb 5 '07 #4

P: n/a
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)
Feb 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.