Behaviour of classes (tired of writing too much)

M

mpn

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
 
B

Brian Quinlan

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)

What do you mean by "used in any way"? Which of these are considered usage:

Anyway, look into __getattr__

Cheers,
Brian
 
S

Steve Holden

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top