Python のデコレータとインスペクトで末尾再帰のループ展開 修正版

id:metanest:20110220:1298203524 のをまともに直した

import inspect

class Thunk(object):
  def __init__(self, f, v):
    self.func = f
    self.args = v

  def force(self):
    return self.func(*self.args)

def loop_ex(func):
  def ex_func(*args):
    framerecords = inspect.stack()
    if len(framerecords) > 2:
      framerecord = framerecords[2]
      try:
        frame = framerecord[0]
        arginfo = inspect.getargvalues(frame)
        binds = arginfo.locals
      finally:
        del frame
      that = binds.get('self')
    else:
      that = None
    if isinstance(that, Thunk):
      return Thunk(func, args)
    else:
      c = Thunk(func, args)
      while isinstance(c, Thunk):
        c = c.force()
      return c

  return ex_func

@loop_ex
def fact(n, acc):
  if n > 0:
    return fact(n - 1, acc * n)
  else:
    return acc

print(fact(5, 1))