Skip to content
Advertisement

How to create a class that doesn’t re-create an object with identical input parameters

I am trying to create a class that doesn’t re-create an object with the same input parameters. When I try to instantiate a class with the same parameters that were used to create an already-existing object, I just want my new class to return a pointer to the already-created (expensively-created) object. This is what I have tried so far:

JavaScript

This is my first venture into overriding __new__ and I’m convinced I’m not going about it the right way. Set me straight, please.

Advertisement

Answer

First, use Upper Case Class Names in Python.

Second, use a Factory design pattern to solve this problem.

JavaScript

This is much simpler than fooling around with new and having class level pools of objects.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement