473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Catching all methods before they execute

I have run into some cases where I would like to run a class method
anytime any class method is invoked.
That is, if I write
x.foo

then it will be the same as writing
x.bar
x.foo

for any method in class x (with the possible exception of 'bar').

The first few times I wanted to print out a data structure for
debugging purposes. Most recently it was to save a data structure to
allow "undo" operations. I've realized that this is a generalized
problem and I was hoping that someone could point me to the correct
hook to accomplish this (python seems to have a hook for everything).

Jul 18 '05 #1
3 1044
ja****************@gmail.com wrote:
I have run into some cases where I would like to run a class method
anytime any class method is invoked.


Perhaps you want __getattribute__ on a new-style class?
--
Michael Hoffman
Jul 18 '05 #2
>jamesthiele.use...@gmail.com wrote:
I have run into some cases where I would like to run a class method
anytime any class method is invoked.


Perhaps you want __getattribute__ on a new-style class?
--
Michael Hoffman


Perhaps I do. The docs say that __getattribute__ is called on all
attribute references, so I tried to make an undoable list as follows:
% cat Undoable.py
class Undoable(object):
def __init__(self, superclass):
self.superclass = superclass
print "__init__"

def __getattribute__(self, name):
print "__getattribute__"
self.SaveState(self)
self.superclass.__getattribute__(self, name)

def SaveState(self):
print "SaveState"

def RestoreState(self):
pass

l = Undoable(list)
l = [1, 2, 3]
print l.count(1)
% python Undoable.py
__init__
1

It appears that __init__ in Undoable is called, count() in list is
called, but not __getattribute__ or SaveState in Undoable.

What don't I understand?

Jul 18 '05 #3
ja****************@gmail.com wrote:
l = Undoable(list)
l = [1, 2, 3]


You just rebound l, so it no longer refers to an Undoable, it
refers to a list. This design won't work, you need something
more like:

l = Undoable([1, 2, 3])

There were a few other pitfalls in your design... Here,
try something like this instead:

class SurrogateNotInitedError(exceptions.AttributeError) :
pass

class Surrogate(object):
"""
the data is stored in _data
list1 = [0, 1, 2, 3]
list2 = [4, 5, 6, 7]
surrogate = Surrogate(list1)
surrogate.reverse()
list1 [3, 2, 1, 0] surrogate._data = list2
surrogate.append(8)
list2

[4, 5, 6, 7, 8]
"""
def __init__(self, data):
self._data = data

def __getattr__(self, name):
if name == "_data":
raise SurrogateNotInitedError, name
else:
try:
return getattr(self._data, name)
except SurrogateNotInitedError:
raise SurrogateNotInitedError, name

You can modify this to make an UndoableSurrogate with appropriate saving
of state. Note the use of __getattr__ instead of __getattribute__. The
latter is not needed since there are not any attributes defined on
Surrogate instead.
--
Michael Hoffman
Jul 18 '05 #4

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

Similar topics

2
by: fred | last post by:
Hello, I read a file from several classes and want to catch an EOF when it appears. Having caught the EOF I want to allow the final part of the program to continue rather than just exiting. ...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
by: Adam | last post by:
If I catch an exception in a static callback method like: private static void ResponseCallback(IAsnycResult asyncResult) { try { //code...code...code } catch (WebException e) { //handle...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
4
by: phlakie | last post by:
I made a call to a stored procedure in a web method. The stored procedure either returns an error or an integer. How do I catch this error in the web method? Thanks.
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
0
by: Peter Morris [Droopy eyes software] | last post by:
I have solved this problem but it involved a static class, reflection, and a lot of code. I wanted to post it here because the solution using virtual class methods takes only a few lines of code....
5
by: Simon Tamman | last post by:
I have an object named DisasterRecovery. The Ctor of this object is this: private DisasterRecovery() { Application.ThreadException+= new...
1
by: Matt Brown - identify | last post by:
Hello, I, as well as suspecting many others, am having an issue when I attempt to enter sleep mode in Vista. If any program is connected to the internet (AOL Instant Messenger for instance),...
0
by: Peter Duniho | last post by:
On Mon, 01 Sep 2008 16:14:10 -0700, Blip <blip@krumpli.comwrote: Briefly, an anonymous method is exactly that: a method without a name. When you use the "delegate" keyword to declare an...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.