Possible PEP: Improve classmethod/staticmethod syntax

Jack Diederich jack at performancedrivers.com
Wed Jun 4 07:33:32 CEST 2003


On Wed, Jun 04, 2003 at 12:18:51AM -0400, Lulu of the Lotus-Eaters wrote:
>     def fooDoc(f):
>         f.__doc__ = "Bob's your uncle."
>         return f
> 
>     class Klass(object):
>         def meth(this, that) [fooDoc]:
>             pass
> 
> Now the method bound to the name Klass.meth is just like the one you
> defined, only its docstring is set to the string "Bob's your uncle".
> OK...  I bet someone can come up with a more *useful* example.

def memoize(f):
  """cache the results of a function for its arguments"""

  cache = {}
  def new_f(*args):
    if (args not in cache):
      cache[args] = f(*args)
    return cache[args]
  return new_f

def long_running_func(*args) [memoize]:
  """this function takes lots of cycles, 
     and only depends on its arguments"""
  
  result = do_lost_of_stuff()
  return result





More information about the Python-list mailing list