Python Forum
3D vector class with inheritance from 2D vector class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3D vector class with inheritance from 2D vector class
#1
I am trying to make a scalar multiplication method which is the last method in the code. I am not sure why it is not working. I used "return D3Vector((self.x * num), (self.y * num), (self.z * num))" It is returning only the new x and y coordinates when I use print(v4.scalarMult(3)), for example.
Here is the full code.
import math
class Vector:
  def __init__(self, x, y):
    self.x = x
    self.y = y
  
  def get_x(self):
    return self.x
  
  def set_x(self, x):
    self.x = x
  
  def get_y(self):
    return self.y
  
  def set_y(self, y):
    self.y = y
  
  def __str__(self):
    return '(' + str(self.x) + ',' + str(self.y) + ')'
  
  def __add__(self, other):
    new_x = self.x + other.x
    new_y = self.y + other.y
    total = Vector(new_x, new_y)
    return total

  def __sub__(self, other):
    new_x = self.x - other.x
    new_y = self.y - other.y
    total = Vector(new_x, new_y)
    return total

  def __mul__(self, other):
    return (self.x * other.x) + (self.y * other.y)
  
  def __abs__(self):
    return math.sqrt((self.x**2) + (self.y**2))

  def __eq__(self, other):
    return self.x == other.x and self.y == other.y

class D3Vector(Vector):
  def __init__(self, x, y, z):
    Vector.__init__(self, x, y)
    self.z = z
  
  def __add__(self, other):
    return (self.x + other.x, self.y + other.y, self.z + other.z)
  
  def __sub__(self, other):
    return (self.x - other.x, self.y - other.y, self.z - other.z)
  
  def __mul__(self, other):
    return Vector.__mul__(self, other) + self.z * other.z
  
  def __abs__(self):
    return math.sqrt(self.x**2 + self.y**2 + self.z**2)

  def __eq__(self, other):
    return Vector.__eq__(self, other) and self.z == other.z

  def scalarMult(self, num):
    return D3Vector((self.x * num), (self.y * num), (self.z * num))


v = Vector(21, 56)
v1 = Vector(13, 24)
v2 = Vector(21, 56)
v3 = D3Vector(3, 6, 9)
v4 = D3Vector(7, 4, 8)
v5 = D3Vector(7, 4, 8)
buran write Dec-20-2020, 04:13 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
D3Vector inherits __str__ from Vector.
Reply
#3
(Dec-20-2020, 02:47 PM)deanhystad Wrote: D3Vector inherits __str__ from Vector.

Thank you!
Reply
#4
What's the need for inheritance here anyway? They are two separate things really and is it really worth it for that small amount of reuse? Are you going to have cases where you make use of polymorphism? Also, some of your methods look like they "work" if used with a Vector and D3Vector, like Vector.__eq__ (in that they return a result), when that doesn't make sense.

Also, D3Vector's __add__ and __sub__ methods look wrong - they're returning tuples instead of D3Vectors.
buran likes this post
Reply
#5
I have the opposite opinion about "Why inheritance?" A Vector is just a 2DVector with Z = 0. Why not just have a Vector class that can be 2D or 3D?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 444 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 271 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 304 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 400 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 569 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 684 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 777 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 491 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,845 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 621 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020