[Tutor] Class causing segmentation fault???

Gary Coulbourne bear@efirms.com
Fri, 21 Jul 2000 14:54:18 -0400


Howdy, folks!  I'm sure there's a better way to do what I'm trying to
do, but if there is, I think it should raise an exception instead of
dumping core...  

In any case, I have the following class in a file called vector.py:

##########################3

from math import *
 
class vector:
 
        v1=0.0 # The x,y,z of the vector
        v2=0.0
        v3=0.0
 
        def __init__(self,v1Value=0.0,v2Value=0.0,v3Value=0.0):
                #Check and see if numbers or a vector was passed
                if type(v1Value)==type(1):
                        self.v1=v1Value
                        self.v2=v2Value
                        self.v3=v3Value
                elif type(v1Value)==type(vector()):
                        self.v1=v1Value.v1
                        self.v2=v1Value.v2
                        self.v3=v1Value.v3
                else:
                        raise "VectorError"
 
        def length(self):
                return
sqrt(pow(self.v1,2)+pow(self.v2,2)+pow(self.v3,2))
 
        def normalize(self):
                len=self.length()
                self.v1=self.v1/len
                self.v2=self.v2/len
                self.v3=self.v3/len
 
        def __repr__(self):
                return "[%d,%d,%d]" % (self.v1,self.v2,self.v3)     

# Build the default axis
xAxis=vector(1,0,0)
yAxis=vector(0,1,0)
zAxis=vector(0,0,1)     

#######################################

Since python doesn't allow overloading (I don't think), I've got the
elif up there so that it checks to see if the type sent in is the same
as a vector instance, and if it is, copy the values.

So, I fire up the python interpreter, and do this:

Python 1.5.2 (#1, May  9 2000, 15:05:56)  [GCC 2.95.3 19991030
(prerelease)] onlinux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from vector import *
>>> a=xAxis
>>> b=vector(a)
Segmentation fault     <---- WOW!

It SegVs.  Drops core.  I was wondering if anyone knew why?  And if
there is a better way to write a constructor to do what I'm attempting
up there.  Thanks!

-- 
Cheers,
   Gary

     *********    *****    **                  Gary Coulbourne
    *************************.*            Director of Tech. R & D
   ****** *********** ** *******o              efirms.com, Inc.
   ******* ********* **** ****`-
   ******* ********* *****                  http://www.efirms.com
    ****** ********** ****                     bear@efirms.com
   ## *****  ***** ##  ****
   ### *****      ###   ****
   #,,, ***,,,    ##,,,  **,,,