Could an object delegate itself into another?

François Pinard pinard at iro.umontreal.ca
Thu Oct 21 15:32:53 EDT 1999


Hi, people.  Here I am, with another question.

In one program, I keep a registry of created objects into a dictionary.
Objects register themselves through their __init__ method.  From outside
the class, I check if a given object has already been created, so to avoid
creating another one that would be too similar, kind of redundant.

It would be neat if I could hide this check.  The ideal would be that the
instantiating call returns an already existing similar object if it exists,
instead of a brand new one.  A simple trick is to not instantiate directly,
but through some service function that does the check, returning either
the old object, or a brand new one.

But yet, I wonder.  Could an object really delegates itself into another?
For example, is there some way by which a newly created object could
soon check, probably within __init__, if a similar copy has already been
registered?  If one similar object is found, this newly created object would
immediately dismiss itself in favour of the previous one, and the genuine
class instantiator would return the previous object.  Is that possible?

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list