Python class which chains Google translations back and forth between two languages I created a class which creates chained translations (for example, Japanese translated to English, then back to Japanese, then to English).

Some concerns:


Is my usage of recursion good?
Is the class pythonic? Is there anything else to make it more pythonic?
I used instantiated Translator twice in my code. Is there any way to only use it once?


Code:

from googletrans import Translator

word_list = []


class Translate:

    def __init__(self, word, target_language='en'):
        translator = Translator()

        self.target_language = target_language
        self.original_language = translator.detect(word).lang
        self.chained_translation = self.repeat_translation(word, target_language)

    def __getitem__(self, item):
        return self.chained_translation[item]

    def __len__(self):
        return len(self.chained_translation)

    def repeat_translation(self, word, target, count=0):
        translator = Translator()
        translated_text = translator.translate(word, dest=target).text
        word_list.append(translated_text)
        count += 1
        # When recursive count is odd.
        if count % 2 == 1:
            return self.repeat_translation(translated_text, self.original_language, count)
        # When recursive count is even and under 10.
        if count &lt; 10:
            return self.repeat_translation(translated_text, self.target_language, count)
        # When recursive ends.
        else:
            return word_list


Example usage:

test = Translate('は忙しい。彼は多忙な人間だ。勉強をしたい')
print(test.chained_translation)
print(test[9])
print(test[8])
print(len(test))

['I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい']
私は忙しいです。彼は忙しい人だ。学びたい
I am busy. He is a busy man. I want to learn
10
I think that you should be more explicit about the source language instead of relying exclusively on auto-detection, in both your Translate constructor and when you call translator.translate().  Japanese is easy enough to detect unambiguously in most cases.  However, phrases in other languages could easily be interpreted as several possible languages.

Recursion is rarely the most idiomatic solution in Python.  Rather, generators are often a better idea.  Here's a generator that will translate back and forth between two languages as many times as you want:

from googletrans import Translator

def chained_translations(phrase, target_language='en', source_language=None):
    translator = Translator()
    source_language = source_language or translator.detect(phrase).lang
    while True:
        phrase = translator.translate(phrase, src=source_language, dest=target_language).text
        yield phrase
        source_language, target_language = target_language, source_language


This solution is much simpler, because the state is kept in the execution flow of the generator code itself.

Example usage:

&gt;&gt;&gt; test = chained_translations('は忙しい。彼は多忙な人間だ。勉強をしたい')
&gt;&gt;&gt; print(next(test))
'I am busy. He is a busy man. I want to learn'
&gt;&gt;&gt; print(next(test))
'私は忙しいです。彼は忙しい人だ。学びたい'
&gt;&gt;&gt; from itertools import islice
&gt;&gt;&gt; print(list(islice(test, 3)))
['I am busy. He is a busy man. I want to learn', '私は忙しいです。彼は忙しい人だ。学びたい', 'I am busy. He is a busy man. I want to learn']