Renaming or Overloading In Python

Paul McGuire ptmcg at austin.rr.com
Sun Mar 18 20:43:42 CET 2007


On Mar 18, 2:17 pm, "gamename" <namesagame-use... at yahoo.com> 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?
>
> 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






More information about the Python-list mailing list