Could an object delegate itself into another?

Hrvoje Niksic hniksic at srce.hr
Thu Oct 21 16:40:02 EDT 1999


François Pinard <pinard at iro.umontreal.ca> writes:

> 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?

The solution that springs to one's mind is to tweak the return value
of __init__().  Unfortunately, anyone who's tried something like that
noticed that Python enforces __init__() to return None.

However, tackling this problem might be an interesting use for
metaclasses (see http://www.python.org/doc/essays/metaclasses/).  I
don't understand metaclasses well enough (if at all) to code this, but
it looks like it should be easy to do.

Anyone wants to try?




More information about the Python-list mailing list