Renaming or Overloading In Python
gamename
namesagame-usenet at yahoo.com
Mon Mar 19 14:16:09 EDT 2007
On Mar 18, 4:57 pm, a... at mac.com (Alex Martelli) wrote:
> 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?
>
> 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
More information about the Python-list
mailing list