One solution for this problem, is to use inheritance, and just change the logic for MyClass
inside of a new class.
Given the following contents of a.py
:
class MyClass:
def __init__(self, x,y,z):
self.x = x
self.y = y
self.z = z
def get(self, somestring):
pipeline = [somestring]
return pipeline
Creating a new object of MyClass
and calling get("somestring")
, will return a list containing just that string. However, we do not set the property inside of the class, so it's only available inside of the method itself.
Creating a new file, in this case b.py
, we can create a new class, inheriting from the first class, and just modify the get
method to have the logic we want.
from a import MyClass
class MyNewClass(MyClass):
def get(self, somestring):
self.pipeline = [somestring]
Inside of b.py
we can then do the following test:
old = MyClass(1,2,3)
print(old.x, old.y, old.z)
print(old.get("this is the input-string"))
new = MyNewClass(4,5,6)
print(new.x, new.y, new.z)
new.get("This is the second input-string")
print(new.pipeline)
Output:
1 2 3
['this is the input-string']
4 5 6
['This is the second input-string']