473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

adding new functionality to a function non-intrusively!

Hello all,

Recently I've started to refactor my code ...
I would like to add extra functionality to a function !non-intrusively!
(not rewriting the old code is really important)
How can I achieve this?

Thus I start with an old function named fA.
I would like to add extra functionality to fA.
Essentially this gives a new function fA which can call the old
function fA

thanks a lot for reading so far

Peter

ps.
following idea does not work!
# fA = old fA, already defined
def tempFunc(*args):
# do something with fA
return
fA = tempFunc # gives infinite loop, recursive mails
# this example is explained in more detail in other mail called:
"renaming 'references' to functions gives recursive problems'

Jul 18 '05 #1
7 1184
Whenever I want to add functionality to a function while still allowing
it to word as it was before my edit would be to include a few optional
variables passed to the string. something to this effect would look
like:

------------------------------------------
BEFORE:
------------------------------------------
def myfunction(a, b):
return (a + b)
------------------------------------------
AFTER:
------------------------------------------
def myfunction(a, b, op="add"):
if (op == "add"):
return (a + b)
else:
return (a - b)
------------------------------------------

Jul 18 '05 #2
Decorators are your friends. You can wrap a function
and give it additional functionality. For instance just
yesterday I needed to keep track of how many times
a function is called. This can be done with this
decorator:

..def with_counter(f):
.. def wrappedf(*args, **kw):
.. wrappedf.counter += 1
.. return f(*args, **kw)
.. wrappedf.counter = 0
.. wrappedf.func_name = f.func_name
.. return wrappedf

..@with_counter # requires Python 2.4
..def g():
.. print "called"

..print g.counter
..g()
..print g.counter

This is intented just as an appetizer. Look at the
cookbook for more examples.

Michele Simionato

Jul 18 '05 #3
Hello

I indeed also rely on your idea.
My problem however is: is has to be non-intrusively in both ways.

In your example, it would indeed not break the old code which relies on
myfunction
but my problems is: I also do not want to change the code of the
'before' myfunction.

so my problem in fact is:
BEFORE:
def myfunction(a,b):
return (a+b)
AFTER:
def myfunction(a,b, op="add"):
if (op == "add"):
# some function which calls the old code for myfunction

kind regards

Jul 18 '05 #4
thx a lot for the information on decorators

this will be very usefull... (sounds like a little step towards aspect
orientated programming,:) )

Because I use libraries which rely on boost-python I can not jump into
python 2.4

but I understand the main idea and can also use it in python 2.3
(it will only cost a little bit more extra coding)

Jul 18 '05 #5

"peter" <Pe*****************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
so my problem in fact is:
BEFORE:
def myfunction(a,b):
return (a+b)
AFTER:
def myfunction(a,b, op="add"):
if (op == "add"):
# some function which calls the old code for myfunction


Is this wrapping what you want?

_myfunction = myfunction
def myfunction(a,b, op='add'):
if (op == 'add'): return _myfunction(a,b)
# else do new stuff and return

Terry J. Reedy

Jul 18 '05 #6
indeed it does, so basically everything works except my original
solution:

def myfunction(a,b):
return a+b

def _myfunction(a,b):
return myfunction(a,b)
myfunction = _myfunction

oh well, it was enough to puzzle my tiny brain....

Jul 18 '05 #7

"peter" <Pe*****************@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
indeed it does, so basically everything works except my original
solution:

def myfunction(a,b):
return a+b

def _myfunction(a,b):
return myfunction(a,b)
myfunction = _myfunction

oh well, it was enough to puzzle my tiny brain....


For newbies reading this, the point is that names within functions,
including global function names, are looked up when and each time the
function is *called*, not when it is defined. So even though _myfunction
does not *look* recursive, since it appears to call some other function,
the name rebinding after *makes* it recursive. Conversely, we can make a
recursive-looking function not recursive.
def f(): return f() # sure looks infinitely recursive .... g = f
f = int # arbitrary choice of function that will accept no args
f() 0 g()

0

Moral: be careful about rebinding function names, and the order in which
you do so.

Terry J. Reedy

Jul 18 '05 #8

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

Similar topics

4
by: Toby A Inkster | last post by:
In "the other place" Jukka has pointed out that semantically <blockquote/> is useless in many browsers, so I'm experimenting with Javascript to put some functionality back into <blockquote/>. ...
15
by: Stormkid | last post by:
Hey Gang, I'm trying to figure out the best way to add two times together of the format hh:mm:ss any suggestions would be great thanks Todd
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: Dmitry Korolyov [MVP] | last post by:
When we use btnSubmit.Attributes = "javascript: this.disabled=true;" to make the button disabled and prevent users from clicking it again while form data still posting, there is no longer...
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
28
by: Ilias Lazaridis | last post by:
I understand that I can use __metaclass__ to create a class which modifies the behaviour of another class. How can I add this metaclass to *all* classes in the system? (In ruby I would alter...
1
by: Subodh | last post by:
Hi, We have legacy code in C/C++, I am writing a wrapper component in C++/ CLI that will allow using this legacy code functionality in C#, I have linked all my static libraries to this C++/CLI...
1
by: jholg | last post by:
Hi, regarding automatically adding functionality to a class (basically taken from the cookbook recipee) and Python's lexical nested scoping I have a question wrt this code: #-----------------...
2
by: Ken Fine | last post by:
I want to add the security question and answer security feature to the ChangePassword control. I am aware that this functionality is built into the PasswordRecovery tool. I have implemented the...
5
by: Joe | last post by:
I'm trying to add the current time + a randomly generated time w/ millisecond precision, but the numbers aren't making sense. If I add rand_time + now_time on my calculator, I get a different...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
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...
0
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...

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.