Renaming or Overloading In Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gamename

    #1

    Renaming or Overloading In Python

    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

  • Diez B. Roggisch

    #2
    Re: Renaming or Overloading In Python

    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

    Comment

    • Paul McGuire

      #3
      Re: Renaming or Overloading In Python

      On Mar 18, 2:17 pm, "gamename" <namesagame-use...@yahoo.co mwrote:
      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._callCou nt += 1
      print "Calling %s for the %d'th time" % (fn.__name__,
      tempFn._callCou nt)
      fn(*args)
      print "Called %s for the %d'th time" % (fn.__name__,
      tempFn._callCou nt)
      tempFn._callCou nt = 0
      tempFn.__name__ = fn.__name__
      return tempFn

      @functionCounte r
      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:


      -- Paul



      Comment

      • Alex Martelli

        #4
        Re: Renaming or Overloading In Python

        gamename <namesagame-usenet@yahoo.co mwrote:
        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

        Comment

        • gamename

          #5
          Re: Renaming or Overloading In Python

          On Mar 18, 4:57 pm, a...@mac.com (Alex Martelli) wrote:
          gamename <namesagame-use...@yahoo.co mwrote:
          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

          Comment

          Working...