0

What should be done to return the same object for the same set of arguments to the constructor instead of creating a new object at every class instantiation call. The code should only create a new object if it finds a new set of arguments in the constructor.

class Test1(object):
    def __init__(self, arg1, arg2, arg3):
        self.arg1 = arg1
        self.arg2 = arg2
        self.arg3 = arg3

    def func1(self):
        pass

    def func2(self):
        pass

if __name__ == "__main__":
    ob1 = Test1("a1", "a2", "a3")
    ob2 = Test1("a1", "a2", "a3")
    ob3 = Test1("b1", "b2", "b3")

    print(ob1 == ob2)     # Prints False
    print(ob1 == ob3)     # Prints False

So, is there any implementation technique or any design pattern which returns the already created object if the same set of arguments are passed, that is, in the above example ob1 and ob2 should point to the same object?

Any help would be appreciated. Thank you.

Kalit Inani
  • 47
  • 1
  • 5

0 Answers0