Не могу понять условие возврата

#python

Вопрос:

 def remove_repeated_characters(tokens):
    repeat_pattern = re.compile(r'(w*)(w)2(w*)')
    match_substitution = r'123'
    
    def replace(old_word):
        if wordnet.synsets(old_word):
            return old_word
        new_word = repeat_pattern.sub(match_substitution, old_word)
        return replace(new_word) if new_word != old_word else new_word
            
    correct_tokens = [replace(word) for word in tokens]
    return correct_tokens
 

Я не понимаю условия возврата replace функции, вы можете мне помочь?

Ответ №1:

это просто означает

 if new_word != old_word:
    return replace(new_word)
else:
    return new_word
 

Ответ №2:

Попробуй это:

 def func(x):
    return 'Yes' if x < 5 else 'No'


for i in range(10):
    print(func(i), end=' ')

print()


def func_new(x):
    if x < 5:
        return "Yes"
    else:
        return "No"


for i in range(10):
    print(func_new(i), end=' ')

>>>Yes Yes Yes Yes Yes No No No No No
>>>Yes Yes Yes Yes Yes No No No No No
 

Состояние там означает:

 if new_word != old_word:
   return replace(new_word)
else:
   return new_word