473,513 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Behaviour of classes (tired of writing too much)

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

Sep 11 '06 #1
2 883
mp*@mic.dtu.dk 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)
What do you mean by "used in any way"? Which of these are considered usage:
>>a = Foo() # obviously
b = a # ?
a.__gt__(5) # apparently
a.__gt__ # ?
Anyway, look into __getattr__

Cheers,
Brian
Sep 11 '06 #2
mp*@mic.dtu.dk 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

Sep 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1692
by: Jean-François Doyon | last post by:
Hello, I'm using MetaClasses to create classes. How do I make these new classes "globally" available? I probably just have to assign them to something magic, but I can't seem to figure out...
48
3036
by: marbac | last post by:
Hi, i heard a lot about "undefined behaviour" in this and other newsgroups dealing with c/c++. Is there a list where all cases with undefined behaviour in C++ are listed? regards marbac
4
1800
by: john townsley | last post by:
do people prefer to design classes for the particular job or for a rangle of tasks they might encounter now and in the future. i am doing some simple win32 apps and picking classes is simple, but...
4
2072
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
2
3019
by: Indiana Epilepsy and Child Neurology | last post by:
Before asking this questions I've spent literally _years_ reading (Meyer, Stroustrup, Holub), googling, asking more general design questions, and just plain thinking about it. I am truly unable to...
10
1779
by: sindica | last post by:
I am using DevC++ 4.0 lately, which uses Mingw port of GCC, on a WinXP. I am surprised to see the malloc behaviour which is not consistent with the documentation. See the program and its output...
31
1327
by: grid | last post by:
Hi, A collegue of mine is of the opinion that the behaviour of the following program is defined,but I am a little apprehensive. #include<stdio.h> #include<string.h> int main() { char *c;
173
5566
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are...
6
4015
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.