python 循环为循环的类,它递归调用一个函数直到满足条件,然后移动到下一个函数并调用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 循环为循环的类,它递归调用一个函数直到满足条件,然后移动到下一个函数并调用相关的知识,希望对你有一定的参考价值。

class Ringloop (object):
	
#"A class that loops as a ring, it recursively calls function one until a condition is True, then recursively calls function two until the second condition is True. The loop keeps iterating in a ring across the two functions until the end condition is satisfied, and the final value is returned."
	
	def __init__(self, value, f1, f2, c1, c2, end):
		self.value = value
		self.f1 = f1 #first function
		self.f2 = f2 #second function
		self.c1 = c1 #condition for checkpoint1
		self.c2 = c2 #condition for checkpoint2
		self.end = end #condition to return the value
	def checkpoint1(self):
		if self.end(self.value) == True:
			Ringloop.endloop(self)
		else: 
			if self.c1(self.value) == True:
				Ringloop.function2(self)
			if self.c1(self.value) == False:
				Ringloop.function1(self)
	def checkpoint2(self):
		if self.end(self.value) == True:
			Ringloop.endloop(self)
		else: 
			if self.c2(self.value) == True:
				Ringloop.function1(self)
			if self.c2(self.value) == False:
				Ringloop.function2(self)
	def function1(self):
		self.value = self.f1(self.value)
		Ringloop.checkpoint1(self)
	def function2(self):
		self.value = self.f2(self.value)
		Ringloop.checkpoint2(self)
	def startloop(self):
		Ringloop.checkpoint1(self)
	def endloop(self):
		print(self.value)
		return self.value


	
	
	

以上是关于python 循环为循环的类,它递归调用一个函数直到满足条件,然后移动到下一个函数并调用的主要内容,如果未能解决你的问题,请参考以下文章

python 函数的进阶

Python基础-----while循环练习

Python|奇/偶数倒数求和之循环与递归的奥秘

Python 3.3 如何将此递归函数转换为纯 yield 循环版本?

python递归函数及二分法查找

Python函数的循环调用