44602 次阅读

Python 尾调用优化decorator

Decorator implements 尾调用优化

#!/usr/bin/env python2.4
# This program shows off a python decorator(
# which implements tail call optimization. It
# does this by throwing an exception if it is 
# it's own grandparent, and catching such 
# exceptions to recall the stack.

import sys

class TailRecurseException:
  def __init__(self, args, kwargs):
    self.args = args
    self.kwargs = kwargs

def tail_call_optimized(g):
  """
  This function decorates a function with tail call
  optimization. It does this by throwing an exception
  if it is it's own grandparent, and catching such
  exceptions to fake the tail call optimization.
  
  This function fails if the decorated
  function recurses in a non-tail context.
  """
  def func(*args, **kwargs):
    f = sys._getframe()
    if f.f_back and f.f_back.f_back \
        and f.f_back.f_back.f_code == f.f_code:
      raise TailRecurseException(args, kwargs)
    else:
      while 1:
        try:
          return g(*args, **kwargs)
        except TailRecurseException, e:
          args = e.args
          kwargs = e.kwargs
  func.__doc__ = g.__doc__
  return func

@tail_call_optimized
def factorial(n, acc=1):
  "calculate a factorial"
  if n == 0:
    return acc
  return factorial(n-1, n*acc)

print factorial(10000)
# prints a big, big number,
# but doesn't hit the recursion limit.

@tail_call_optimized
def fib(i, current = 0, next = 1):
  if i == 0:
    return current
  else:
    return fib(i - 1, next, current + next)

print fib(10000)
# also prints a big number,
# but doesn't hit the recursion limit.

相关文章

已有 807 条评论
  1. JasonWig

    phenergan buy online tetracycline prescription propecia tadalafil lisinopril

    JasonWig 回复
  2. MiaWig

    propecia phenergan cialis canada pharmacy buy tadalafil cialis tetracycline prescription

    MiaWig 回复
  3. AnnaWig

    ventolin hfa buy tadalafil propecia how much is metformin buy prednisolone lisinopril viagra without prescription buy tetracycline without prescription cilais buy phenergan online

    AnnaWig 回复
  4. AnnaWig

    tadalafil ventolin phenergan 50 mg buying prednisoline tablets buy propecia no prescription buy viagra online without prescription canada cialis online lisinopril pill metformin buy tetracycline without prescriptions

    AnnaWig 回复
  5. Sonia97bed

    homepage site - our website

    Sonia97bed 回复
  6. SamWig

    prednisolone tadalafil best price generic for cialis propecia phenergan metformin generic lisinopril 15 mg ventolin no prescription order tetracycline online viagra without prescription

    SamWig 回复
  7. JoeWig

    viagra without a prescription prednisolone order tetracycline without a prescription canada pharmacy cialis buy phenergan buy propecia lisinopril ventolin drug tadalafil metformin

    JoeWig 回复
  8. AnnaWig

    propecia phenergan tetracycline cialis canada online pharmacy order metformin on line buy lisinopril online tadalafil prednisolone non prescription viagra ventolin

    AnnaWig 回复
  9. IvyWig

    cheapest tadalafil buy prednisolone online tetracyline viagra no prescription buy propecia

    IvyWig 回复
  10. JimWig

    propecia tadalafil metformin prednisolone phenergan lisinopril ventolin canada cialis online tetracycline viagra without prescription

    JimWig 回复
  11. auto insurance quote

    geico car insurance

    auto insurance quote 回复
  12. best insurance

    insurance car salvage

    best insurance 回复
  13. nj auto insurance

    auto insurance

    nj auto insurance 回复
  14. AmyWig

    prednisolone

    AmyWig 回复
  15. IvyWig

    phenergan prices buy tetracycline without prescription lisinopril 20 mg no prescription prednisolone buy no prescription ventolin inhaler

    IvyWig 回复
  16. JasonWig

    cialis canada generic generic viagra lowest price ventolin hfa price metformin 500 prednisolone

    JasonWig 回复
  17. EyeWig

    no prescription ventolin hfa i need viagra buy propecia without prescription tadalafil tablets 20mg canada pharmacy cialis

    EyeWig 回复
  18. cheap car insurance

    auto insurance

    cheap car insurance 回复
  19. KimWig

    no prescription lisinopril

    KimWig 回复
  20. MiaWig

    buying cialis in canada phenergan price lisinopril 20 mg no prescription buy propecia no prescription tetracycline online

    MiaWig 回复
发表新评论