Renaming or Overloading In Python

G

gamename

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
 
D

Diez B. Roggisch

gamename said:
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
 
P

Paul McGuire

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
 
A

Alex Martelli

gamename said:
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
 
G

gamename

gamename said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top