Renaming or Overloading In Python

Alex Martelli aleax at
Mon Mar 19 00:57:25 CET 2007

gamename <namesagame-usenet at> wrote:

> 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.


More information about the Python-list mailing list