[EMAIL PROTECTED] wrote: > How do I catch any reference to an instance of a class, i.e., I want to > run some code as soon as an instance of a class is used in any way. > (and I don't want to define all of __add__, __ge__ etc etc etc etc etc) > > The reason for the question is that I want to simplify the Message > Passing Interface (MPI) calls. I find it irritating that I have to > start a non-blocking send/receive and then check that the data has > arrived. Why not automagically check/wait for the data when it is > needed? > > The second part that I also need to make that idea work is the ability > to change the instance to the received data, i.e., self=received data, > and then launch the original reference. > > So I guess that the code I want to write is: > > class MPI_tmp: > # temporary object that changes ITSELF into another object when > called in any way > def __on_any_call__(self): > self=newobject() > self.__run_the_anycall__() > > which probably doesn't work :-) > > Greatful for any ideas / Magnus > Maybe it could make a damned fine cup of coffee as well ;-) ?
That's a pretty tall order, and mutating self isn't as simple as you think. However you *can* change an instance's __class__ attribute dynamically, which might be a way to get where you want. Not sure about the "trapping all accesses" bit, though. __getattr__() could give you access to undefined attributes. For new_style classes you can implement __getattribute__() to trap *any* attribute access. This would be somewhat slow, but might be acceptable if you were then changing the instance's class to something that *didn't* implement __getattribute__(). regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://holdenweb.blogspot.com Recent Ramblings http://del.icio.us/steve.holden -- http://mail.python.org/mailman/listinfo/python-list