To do tail recursion with Python2

To find the best way to do it by revisiting the end, create a class and perform meta programming starting with @. (ref: http://code.activestate.com/recipes/496691/)

tail_recursive.py


class tail_recursive (object):

  def __init__(self, func):
    self.func = func
    self.firstcall = True
    self.CONTINUE = object()

  def __call__(self, *arguments, **keywords):
    if self.firstcall:
      func = self.func
      CONTINUE = self.CONTINUE
      self.firstcall = False
      try:
        while True:
          result = func(*arguments, **keywords)
          if result is CONTINUE: # update arguments
            arguments, keywords = self.argskwd
          else: # last call
            return result
      finally:
        self.firstcall = True
    else: # return the arguments of the tail call
      self.argskwd = arguments, keywords
      return self.CONTINUE

Use this as @tail_recursive. Tatoheba Next:

@tail_recursive
def sum(n, acc=0):
  if n == 0:
    return acc
  else:
    return sum(n - 1, acc + n)

@tail_recursive You can move without it. But that stack overflows soon.

Recommended Posts

To do tail recursion with Python2
What to do with PYTHON release?
How to do portmanteau test with python
Do Houdini with Python3! !! !!
How to do multi-core parallel processing with python
Try to factorial with recursion
Connect to BigQuery with Python
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Do Django with CodeStar (Python3.8, Django2.1.15)
Connect to Wikipedia with Python
Post to slack with Python 3
Switch python to 2.7 with alternatives
Write to csv with Python
How to do hash calculation with salt in Python
[ML Ops] I want to do multi-project with Python
[Python] How to do PCA in Python
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
What to do with Magics install
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
Convert list to DataFrame with python
How to get started with Python
How to do Bulk Update with PyMySQL and notes [Python]
Let's do image scraping with Python
Unable to install Python with pyenv
How to use FTP with Python
How to calculate date with python
I want to do ○○ with Pandas
Easily post to twitter with Python 3
I want to debug with Python
Python | What you can do with Python
What to do if you can't install pyaudio with pip #Python
Try to reproduce color film with Python
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
English speech recognition with python [speech to text]
Convert memo at once with Python 2to3
HTML email with image to send with python
Memo to ask for KPI with python
Python to remember only with hello, worlds
How to do R chartr () in Python
Output color characters to pretty with python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Output Python log to console with GAE
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
I wanted to solve ABC160 with Python
Connect to MySQL with Python within Docker
How to work with BigQuery in Python
[Introduction to Python] Let's use foreach with Python
Single pixel camera to experience with Python
Convert FX 1-minute data to 5-minute data with Python
I want to analyze logs with Python
I want to play with aws with python
How to display python Japanese with lolipop
Trying to handle SQLite3 with Python [Note]