1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python Vector Class no attribute error

  1. Aug 5, 2012 #1
    Hello all, my issue is in calling the attributes of a 3d vector class in python, this is the class I was working with:

    import math

    class Vec3:
    ''' A three dimensional vector '''
    def __init__(self, v_x=0, v_y=0, v_z=0):
    self.set( v_x, v_y, v_z )
    def set(self, v_x=0, v_y=0, v_z=0):
    if isinstance(v_x, tuple) or isinstance(v_x, list):
    self.x, self.y, self.z = v_x
    else:
    self.x = v_x
    self.y = v_y
    self.z = v_z
    def __getitem__(self, index):
    if index==0: return self.x
    elif index==1: return self.y
    elif index==2: return self.z
    else: raise IndexError("index out of range for Vec3")
    def __mul__(self, other):
    '''Multiplication, supports types Vec3 and other
    types that supports the * operator '''
    if isinstance(other, Vec3):
    return Vec3(self.x*other.x, self.y*other.y, self.z*other.z)
    else: #Raise an exception if not a float or integer
    return Vec3(self.x*other, self.y*other, self.z*other)
    def __div__(self, other):
    '''Division, supports types Vec3 and other
    types that supports the / operator '''
    if isinstance(other, Vec3):
    return Vec3(self.x/other.x, self.y/other.y, self.z/other.z)
    else: #Raise an exception if not a float or integer
    return Vec3(self.x/other, self.y/other, self.z/other)
    def __add__(self, other):
    '''Addition, supports types Vec3 and other
    types that supports the + operator '''
    if isinstance(other, Vec3):
    return Vec3( self.x + other.x, self.y + other.y, self.z + other.z )
    else: #Raise an exception if not a float or integer
    return Vec3(self.x + other, self.y + other, self.z + other)
    def __sub__(self, other):
    '''Subtraction, supports types Vec3 and other
    types that supports the - operator '''
    if isinstance(other, Vec3):
    return Vec3(self.x - other.x, self.y - other.y, self.z - other.z)
    else: #Raise an exception if not a float or integer
    return Vec3(self.x - other, self.y - other, self.z - other )
    def __abs__(self):
    '''Absolute value: the abs() method '''
    return Vec3( abs(self.x), abs(self.y), abs(self.z) )
    def __neg__(self):
    '''Negate this vector'''
    return Vec3( -self.x, -self.y, -self.z )
    def __str__(self):
    return '<' + ','.join(
    [str(val) for val in (self.x, self.y, self.z) ] ) + '>'
    def __repr__(self):
    return str(self) + ' instance at 0x' + str(hex(id(self))[2:].upper())
    def length(self):
    ''' This vectors length'''
    return math.sqrt( self.x**2 + self.y**2 + self.z**2 )
    def length_squared(self):
    ''' Returns this vectors length squared
    (saves a sqrt call, usefull for vector comparisons)'''
    return self.x**2 + self.y**2 + self.z**2
    def cross(self, other):
    '''Return the cross product of this and another Vec3'''
    return Vec3( self.y*other.z - other.y*self.z,
    self.z*other.x - other.z*self.x,
    self.x*other.y - self.y*other.x )
    def dot(self, other):
    '''Return the dot product of this and another Vec3'''
    return self.x*other.x + self.y*other.y + self.z*other.z
    def normalized(self):
    '''Return this vector normalized'''
    return self/self.length()
    def normalize(self):
    '''Normalize this Vec3'''
    self /= self.length()

    With this written as a script I imported it into IPython thusly:

    from Vec3 import *

    v = Vec3(3,4,5)

    v.add(3)
    ---------------------------------------------------------------------------
    AttributeError Traceback (most recent call last)
    C:\Users\CHUCK\Documents\<ipython-input-7-4b1689e130e9> in <module>()
    ----> 1 v.add(3)

    AttributeError: Vec3 instance has no attribute 'add'

    As you can see from the code above, add is clearly an attribute of class Vec3.

    So my question is how can I call the functions of my class without this error?

    Am I calling the class correctly?

    Any and all help is appreciated.
     
  2. jcsd
  3. Aug 6, 2012 #2

    Mark44

    Staff: Mentor

    The Vec3 class does not have an add function - however, it has a function named __add__

    Code (Text):

    def __add__(self, other):
    '''Addition, supports types Vec3 and other
    types that supports the + operator '''
    if isinstance(other, Vec3):
    return Vec3( self.x + other.x, self.y + other.y, self.z + other.z )
    else: #Raise an exception if not a float or integer
    return Vec3(self.x + other, self.y + other, self.z + other)
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Python Vector Class no attribute error
  1. C++ card class errors (Replies: 28)

  2. Python array error (Replies: 5)

Loading...