Hi everybody,
I would like to be able to specialize an existing class A, so as to obtain a
class B(A), with all methods of B being the methods of A preceded by a
special method of B called _before_any_method_of_A( self ), and followed by
a special method of B called _after_any_method_of_A( self ).
The goal is to avoid to redefine explicitly in B all methods of A.
Is this possible in Python?
Thanks a lot
Julien
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"
"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke) 4 1232
On Oct 3, 9:03*am, TP <Tribulati...@Paralleles.invalidwrote:
Hi everybody,
I would like to be able to specialize an existing class A, so as to obtain a
class B(A), with all methods of B being the methods of A preceded by a
special method of B called _before_any_method_of_A( self ), and followed by
a special method of B called _after_any_method_of_A( self ).
The goal is to avoid to redefine explicitly in B all methods of A.
Is this possible in Python?
Thanks a lot
Julien
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"
"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
Yes it's possible. The exact solution depends on how much control you
want, and over what. Do you want inheritance or delegation? Do you
want the same pre and post method for all inherited methods? Will you
always use single-inheritance? Would you mind using a list of names,
as a sort of midpoint between complete redefinition and completely
dynamic? Do you want class lookups to succeed? If so, what do you
want them to do?
Generally, you could build the class dynamically with a metaclass, or
use __getattr__ a lookup.
En Fri, 03 Oct 2008 11:03:22 -0300, TP <Tr**********@paralleles.invalid>
escribió:
I would like to be able to specialize an existing class A, so as to
obtain a
class B(A), with all methods of B being the methods of A preceded by a
special method of B called _before_any_method_of_A( self ), and followed
by
a special method of B called _after_any_method_of_A( self ).
The goal is to avoid to redefine explicitly in B all methods of A.
Is this possible in Python?
Sure. After reading this (excelent!) article by M. Simionato http://www.phyast.pitt.edu/~micheles...mentation.html you should
be able to write a decorator to make any method into a "sandwich"
(probably based on his "trace" example). Your code would look like this:
@decorator
def sandwich(f, self, *args, **kw):
self.before()
f(self, *args, **kw)
self.after()
class A:
@sandwich
def foo(self):
...
@sandwich
def bar(self, x):
...
Ok, but then you have to explicitely decorate every method. To avoid this,
you may use a metaclass; this article by Michael Foord explains how: http://www.voidspace.org.uk/python/a...ting-metaclass
That's all!
--
Gabriel Genellina
Gabriel Genellina wrote:
En Fri, 03 Oct 2008 11:03:22 -0300, TP <Tr**********@paralleles.invalid>
escribió:
>I would like to be able to specialize an existing class A, so as to obtain a class B(A), with all methods of B being the methods of A preceded by a special method of B called _before_any_method_of_A( self ), and followed by a special method of B called _after_any_method_of_A( self ).
The goal is to avoid to redefine explicitly in B all methods of A.
Is this possible in Python?
Sure. After reading this (excelent!) article by M. Simionato http://www.phyast.pitt.edu/~micheles...mentation.html you
should be able to write a decorator to make any method into a "sandwich"
(probably based on his "trace" example). Your code would look like this:
@decorator
def sandwich(f, self, *args, **kw):
self.before()
f(self, *args, **kw)
self.after()
class A:
@sandwich
def foo(self):
...
@sandwich
def bar(self, x):
...
Ok, but then you have to explicitely decorate every method. To avoid
this, you may use a metaclass; this article by Michael Foord explains how: http://www.voidspace.org.uk/python/a...ting-metaclass
I believe this would work (untested, 3.0):
class A(): # define methods
class B(): pass
func = type(lambda:None)
for item in A.__dict__:
if isinstance(item, func):
setattr(B, item.__name__, sandwich(item))
tjr
On Fri, 03 Oct 2008 16:03:22 +0200, TP wrote:
Hi everybody,
I would like to be able to specialize an existing class A, so as to
obtain a class B(A), with all methods of B being the methods of A
preceded by a special method of B called _before_any_method_of_A( self
), and followed by a special method of B called _after_any_method_of_A(
self ).
The goal is to avoid to redefine explicitly in B all methods of A.
Is this possible in Python?
You may be able to adapt this code to do what you are after: http://code.activestate.com/recipes/91192/
--
Steven This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David Chandler |
last post by:
Let me try to describe the situation as clearly as I can.
In namespace XXX I have a class PARENT with the following public
functions:
virtual void foo( const char* stringBuf ); // set...
|
by: Thomas |
last post by:
Hi all,
I want to execute a new program in a c++ program without
beeing blocked. After the child process is created both
processes should run idependend.
My method at the moment:
execute(...
|
by: scott |
last post by:
hi all, hope some one can help me. Ill try and explain what im trying to do
as best i can.
i have a parent class that has a vertual function, lets call it virtual int
A(). That vertual function...
|
by: PyPK |
last post by:
Hi if I have a function called
tmp=0
def execute():
tmp = tmp+1
return tmp
also I have
def func1():
execute()
....
|
by: David N |
last post by:
Hi All,
I just wonder if in C#, I can develop a user defined control that can call
its parent function which is not yet developed. For example, how do I make
my user control call a...
|
by: Keith Chadwick |
last post by:
I have several small pieces of common code that get applied to each page.
Which is better a better way of including them in other pages? Use the
traditional include statement or go with the...
|
by: jw56578 |
last post by:
Is it possible to call a method from a parent object, but have the
childs overriden method called instead of the base method?
I want several children objects to all call a certain method when they...
|
by: msxkim |
last post by:
How to execute functions in the parent class first and then functions
in the child class? For example, I have a parent class with functions
'ONE' and 'TWO' and child class has a function 'THREE'. ...
|
by: Noozer |
last post by:
I have two Classes in use in an ASP application...
The first Class is named "ORDER". It represents one job order. It has a
Delete method, among others, which deletes it from my database.
The...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |