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
  Python inner classes inheritance from parent class Abedin 8 632 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Accessing method attributes of python class Abedin 6 890 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,256 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,425 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 997 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 3,789 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  [split] Class and methods ebn852_pan 15 3,345 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,319 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 1,126 May-13-2024, 04:04 AM
Last Post: deanhystad
  How does this code create a class? Pedroski55 6 2,075 Apr-21-2024, 06:15 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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