By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,165 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,165 IT Pros & Developers. It's quick & easy.

Renaming or Overloading In Python

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.