473,473 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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):
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
4 2432
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Junior | last post by:
I keep receiving this "The type or namespace name 'CASsEventHandler' could not be found (are you missing a using directive or an assembly reference?)" message in two particular lines, and I've...
4
by: Conrad Weyns | last post by:
vc7.1 has no problem with the following snippet: template <typename T> class TTest { std::list<T*>::iterator m_It; }; but metrowerks codewarrior 9.2 kicks my butt and online Comeau confirms:...
2
by: kalpana.sinduria | last post by:
Hi all, how to remove the following complle error. When I compiling the code I get the following errors: Compiling... CDrtEachDefFeat.cpp d:\ include\common\cdrtintegfeat.h(39) : error...
4
by: Peter Olcott | last post by:
Yesterday I found that an invocation of a member function that lacked the required "()" pararenthesis did not generate an error message. This was an invocation of a member function that is part of...
2
by: Caladin | last post by:
I'm sure once someone answers this I'll feel really dumb. I'm a c++ programmer playing with c# an here's my conundrum I declare a class in the for public class Form1 :...
0
by: Harald Hoyer | last post by:
My application has an exe and some DLLs. During debugging only debug information of the current binary is available. For example: ============DLL: ...
14
by: tshad | last post by:
I am trying to set up a reusable library (business component) that I can use in a bunch of my pages. I built the file and it almost compiles, but I am not sure what I am missing. The vbc...
1
by: BobPaul | last post by:
I'm following code out of a howto book and this is really bugging me. This header file was created by VStudio 6.0 when I did a "Right Click: Add Member Function" CLine is a class I wrote (per the...
0
by: RickVidallon | last post by:
Missing or Truncated Body Text in Email Application - 2 Strange Examples... There is no earthly reason why this is happening! EXAMPLES HERE: http://65.36.227.70/actmailer/ We have a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.