473,387 Members | 1,493 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,387 software developers and data experts.

Renaming or Overloading In Python

Hi,

I'm a recent convert from TCL. One of the more powerful aspects of
TCL is the ability to rename a function at will (generally for testing
purposes).

Example from the tcl doc:

rename ::source ::theRealSource
set sourceCount 0
proc ::source args {
global sourceCount
puts "called source for the [incr sourceCount]'th time"
uplevel 1 ::theRealSource $args
}

So, is such a thing possible in Python?

Thanks,
-T

Mar 18 '07 #1
4 1503
gamename schrieb:
Hi,

I'm a recent convert from TCL. One of the more powerful aspects of
TCL is the ability to rename a function at will (generally for testing
purposes).

Example from the tcl doc:

rename ::source ::theRealSource
set sourceCount 0
proc ::source args {
global sourceCount
puts "called source for the [incr sourceCount]'th time"
uplevel 1 ::theRealSource $args
}

So, is such a thing possible in Python?

def foo():
print 'foo'

bar = foo

bar()

Is it that what you mean?

Diez
Mar 18 '07 #2
On Mar 18, 2:17 pm, "gamename" <namesagame-use...@yahoo.comwrote:
Hi,

I'm a recent convert from TCL. One of the more powerful aspects of
TCL is the ability to rename a function at will (generally for testing
purposes).

Example from the tcl doc:

rename ::source ::theRealSource
set sourceCount 0
proc ::source args {
global sourceCount
puts "called source for the [incr sourceCount]'th time"
uplevel 1 ::theRealSource $args

}

So, is such a thing possible in Python?

Thanks,
-T
If you do this sort of thing a lot, you might read up on Python
decorators. Using a simple syntax, you can easily wrap a function
with debugging/logging, synchronization, or any other wrapper
behavior. Here is your function counter implemented as a decorator:

def functionCounter(fn):
fname = fn.__name__
def tempFn(*args):
tempFn._callCount += 1
print "Calling %s for the %d'th time" % (fn.__name__,
tempFn._callCount)
fn(*args)
print "Called %s for the %d'th time" % (fn.__name__,
tempFn._callCount)
tempFn._callCount = 0
tempFn.__name__ = fn.__name__
return tempFn

@functionCounter
def foo():
print "body of foo"

foo()
foo()
foo()

Prints out:
Calling foo for the 1'th time
body of foo
Called foo for the 1'th time
Calling foo for the 2'th time
body of foo
Called foo for the 2'th time
Calling foo for the 3'th time
body of foo
Called foo for the 3'th time
What is happening here is that, at module import time, the function
functionCounter is called, passing it the function foo. The @
decorator is a shortcut for this statement, entered after foo is
defined:

foo = functionCounter(foo)

You can find more decorators at the Python wiki:
http://wiki.python.org/moin/PythonDecoratorLibrary

-- Paul

Mar 18 '07 #3
gamename <na***************@yahoo.comwrote:
Hi,

I'm a recent convert from TCL. One of the more powerful aspects of
TCL is the ability to rename a function at will (generally for testing
purposes).

Example from the tcl doc:

rename ::source ::theRealSource
set sourceCount 0
proc ::source args {
global sourceCount
puts "called source for the [incr sourceCount]'th time"
uplevel 1 ::theRealSource $args
}

So, is such a thing possible in Python?
Assuming that source is a function previously defined in this scope, the
exactly equivalent Python snippet should be:

theRealSource = source
sourceCount = 0
def source(*args):
global sourceCount
sourceCount += 1
print "called source for the %s'th time" % sourceCount
return theRealSource(*args)

Others have already offered you other alternatives, but I thought you
might also be interested in a more direct/immediate translation.
Alex
Mar 19 '07 #4
On Mar 18, 4:57 pm, a...@mac.com (Alex Martelli) wrote:
gamename <namesagame-use...@yahoo.comwrote:
Hi,
I'm a recent convert from TCL. One of the more powerful aspects of
TCL is the ability to rename a function at will (generally for testing
purposes).
Example from the tcl doc:
rename ::source ::theRealSource
set sourceCount 0
proc ::source args {
global sourceCount
puts "called source for the [incr sourceCount]'th time"
uplevel 1 ::theRealSource $args
}
So, is such a thing possible in Python?

Assuming that source is a function previously defined in this scope, the
exactly equivalent Python snippet should be:

theRealSource = source
sourceCount = 0
def source(*args):
global sourceCount
sourceCount += 1
print "called source for the %s'th time" % sourceCount
return theRealSource(*args)

Others have already offered you other alternatives, but I thought you
might also be interested in a more direct/immediate translation.

Alex
Excellent! Thanks guys, that really gives me a place to start.

-T

Mar 19 '07 #5

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

Similar topics

1
by: Fuzzyman | last post by:
I've been programming in python for a few months now - and returning to programming after a gap of about ten years I've really enjoyed learning python. I've just made my first forays into...
7
by: Doran_Dermot | last post by:
Hi All, I've seen lots of code in which the attributes of a class are accessed and modified using two separate methods. For example: class Problems: def __init__( self, refNum ):...
9
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves...
3
by: anton muhin | last post by:
Dear pythonistas! I'd like to emulate overloading in Python (like C++). Let's consider an example: class A(object): pass class B(object): pass Of course, there are some standard...
3
by: Iyer, Prasad C | last post by:
I am new to python. I have few questions a. Is there something like function overloading in python? b. Can I overload __init__ method Thanks in advance regards
12
by: Achim Domma | last post by:
Hi, I want to use Python to script some formulas in my application. The user should be able to write something like A = B * C where A,B,C are instances of some wrapper classes. Overloading...
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
10
by: looping | last post by:
Hi, for the fun I try operator overloading experiences and I didn't exactly understand how it works. Here is my try: def __pow__(self, value): return self.__add__(value) 6
2
by: jimzat | last post by:
I am trying to simulate the execution of some PLC ladder logic in python. I manually modified the rungs and executed this within python as a proof of concept, but I'd like to be able to skip the...
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.