hello,
i used a personal overload library because i can not find a way to overload operators with ‘multimethod’ or other libraries, here is is the problem explained with ‘multimethod’ :
The problem comes from the fact that it is difficult to overload a callable (function,method ,operator…) in a class based on the type annotation as in the class definition, the class type is not already know.
Here is an example:
from multimethod import multimethod
class Matrix3x3():
'''Construct an object Matrix3x3.'''
@multimethod
def __init__(self : Matrix3x3):
self.M = [[0.0,0.0,0.0],
[0.0,0.0,0.0],
[0.0,0.0,0.0]]
in the above example i will overload init method of class Matrix3x3, for example i can have another method like that:
@multimethod
def __init__(self : Matrix3x3,v1 : Vector3D,v2 : Vector3D,v3 : Vector3D):
print("Matrix3x3.py : __init__(Matrix3x3,Vector3D,Vector3D,Vector3D)")
if __debug__:
print("Matrix3x3.py : __init__(Matrix3x3,Vector3D,Vector3D,Vector3D) : v1.x=")
print(v1.x)
print("Matrix3x3.py : __init__(Matrix3x3,Vector3D,Vector3D,Vector3D) : v2.x=")
print(v2.x)
print("Matrix3x3.py : __init__(Matrix3x3,Vector3D,Vector3D,Vector3D) : v2.y=")
print(v2.y)
print("Matrix3x3.py : __init__(Matrix3x3,Vector3D,Vector3D,Vector3D) : v2.z=")
print(v2.z)
self.M = [[v1.x,v1.y,v1.z],
[v2.x,v2.y,v2.z],
[v3.x,v3.y,v3.z]]
if __debug__:
print("# Matrix3x3 constructor #")
but the problem is that Matrix3x3 used to annotate self is not know at this step of definition and here is the error message:
Traceback (most recent call last):
File "/Users/mattei/Dropbox/git/vision3D_python/Vision3D.py", line 13, in <module>
from Matrix3x3 import Matrix3x3
File "/Users/mattei/Dropbox/git/vision3D_python/Matrix3x3.py", line 42, in <module>
class Matrix3x3():
File "/Users/mattei/Dropbox/git/vision3D_python/Matrix3x3.py", line 47, in Matrix3x3
def __init__(self : Matrix3x3):
NameError: name 'Matrix3x3' is not defined
okay i had the same problem with my personal library, i change my library code to allow definition such as ‘Matrix3x3’ as a string and not a real known type.
Another solution was to create a sort of “abstract” class before the real one just for the time of “compilation” of the Matrix3x3 like that:
# this is "like" a type definition to avoid error of undefined type during definition of the final Class
# ,it is an "Abstract" class that will be overwritten by the final one but used
# to pre-define the type Matrix3x3 and use it in the latter definition of Matrix3x3 itself.
class Matrix3x3:
pass
class Matrix3x3():
'''Construct an object Matrix3x3.'''
@multimethod
def __init__(self : Matrix3x3):
self.M = [[0.0,0.0,0.0],
[0.0,0.0,0.0],
[0.0,0.0,0.0]]
if __debug__:
print("# Matrix3x3 constructor #")
this times it is ok for “compiling” the Python code but i then have a problem at execution:
m=Matrix3x3()
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
m=Matrix3x3()
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/multimethod/__init__.py", line 313, in __call__
func = self[tuple(func(arg) for func, arg in zip(self.type_checkers, args))]
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/multimethod/__init__.py", line 307, in __missing__
raise DispatchError(msg, types, keys)
multimethod.DispatchError: ('__init__: 0 methods found', (<class 'Matrix3x3.Matrix3x3'>,), [])
WHY is my Matrix3x3 method not found by multimethod ?
regards,
Damien Mattei