I am working with a piece of code that looks like this:
# This code is not modifiable
from package import distance as dist
class A:
def calculate(self):
...
# call to dist()
...
My code:
from package import A
a = A()
a.calculate()
As you can see, the distance() function is imported at the top of the code. The class A makes a call to a distance() function. It does so, in several places and not only in calculate().
I want the class to use my custom distance function. However, the class does not let me pass it in the constructor and I cannot modify the code of A. How would I do this? Is this possible via subclassing? I tryed the following, which did not work:
from package import A
class B(A):
def __init__(self):
from mypackage import mydistance as dist
return super().__init__()
b = B()
b.calculate()

packageearlier in the python path? can you changepackage?import package; package.dist = lambda x: 10Then try running the code again and see if it was updated