2

I would like to inherit from Vector, a FreeCAD python class.

In the following test code, I either use my own Vector class or import the Vector class from FreeCAD. It works fine with my own Vector class, but prints a vector of zeroes and then crashes with - 'Base.Vector' has no attribute 'extra', implying the constructor of ExtraVector has been bypassed and ExtraVector is just a Vector, when using the FreeCAD Vector.

(I suspect that the FreeCAD vector is a wrapper around C code)

  1. What have FreeCAD done? How do you mess with a python class to give it this behavior?

  2. How do I get around it? Inheritance is the best solution to my problem. Composition means writing a lot more code, and I don't like WET.

# ---------------------------------------------------

# Vector is either defined in this file or imported
# (comment out to swap over)

#from FreeCAD import Vector  # Probably a wrapper around C code

class Vector :  
  def __init__ ( self, x, y, z ) :
    self.x = x
    self.y = y
    self.z = z

# ---------------------------------------------------

class ExtraVector ( Vector ) :
  def __init__ ( self, x, y, z, extra ) :
    super().__init__ ( x, y, z )
    self.extra = extra

def Test ( ) :

  evect = ExtraVector ( 1, 2, 3, 4 )

  print ( "x = ", evect.x,  " y = ", evect.y, " z = ", evect.z )
  print ( "e = ", evect.extra )

Test()

When my Vector class is used (FreeCAD Vector commented out), output is :-

x =  1  y =  2  z =  3
e =  4

When FreeCAD Vector is used (my Vector commented out), output is:-

x =  0.0  y =  0.0  z =  0.0
Traceback (most recent call last):
  File "ExtraVector.py", line 28, in <module>
    Test()
  File "ExtraVector.py", line 26, in Test
    print ( "e = ", evect.extra )
<class 'AttributeError'>: 'Base.Vector' object has no attribute 'extra'

Pete D.
  • 165
  • 1
  • 12
  • If I had to guess, metaclasses and subclass hooks – Mad Physicist Jan 27 '21 at 13:43
  • Your question is not clear. When you extend a class, you add functionality to the new class not to the superclass. – mnesarco Feb 01 '21 at 16:17
  • @mnesarco Yes. Assume there is no FreeCAD. My example code shows what I want. Now, adding FreeCAD, why won't it work with the FreeCAD vector? An explanation would be nice, a workaround would be better. – Pete D. Feb 03 '21 at 12:08
  • What does the code look like when you use `FreeCAD.Vector()`? – Richard Chambers Feb 03 '21 at 12:22
  • @Richard Chambers It's in the example code above. Comment in the FreeCAD import, comment out my class Vector, and rerun. You get the second output above. – Pete D. Feb 03 '21 at 12:33
  • My little test shows that init is not even called. – tito Feb 03 '21 at 13:17
  • @PeteD. you have a big confusion with OOP inheritance. You have extended the Base.Vector class, so you have created a new class ExtraVector, your new class inherits functionality from Base.Vector not the other way. Inheritance is a one way relation. So you must not expect any change in Base.Vector behaviour. – mnesarco Feb 04 '21 at 01:57
  • @mnesarco You are wrong about my code, and introducing a red herring anyway. How do I get the FreeCAD Vector to behave like the test Vector above so they work the same with ExtraVector? – Pete D. Feb 04 '21 at 13:23
  • @PeteD. You cannot modify the FreeCAD's Vector class. You cannot modify the class that you are extending. You are confused about that, but if you feelm that I am introducing a red herring, I am sorry, Go your way and good luck with that. – mnesarco Feb 04 '21 at 15:11
  • @PeteD. The reason why you are not getting useful answers is because your question is not clear. Apparently I missundersud your isssue, on a second analysis, the behaviour described seems to indicate tha FreeCAD's Vector class is final. I am not 100% sure abut that but some Python Wrappers around C++ classes has limited capabilities. – mnesarco Feb 04 '21 at 15:36

0 Answers0