如何在4个内部类的函数中调用函数


class Algorithms:
class SearchAlgorithms:
class RecursiveBinarySearch:
def RecursiveBinarySearchAlgo(List, Target):
'''
Return true value if it exists and a false if it doesn't
'''
# If the length of the List is equal to 0 : return False
if len(List) == 0:
return False
# Else if the list is not empty
else:
Midpoint = (len(List))//2
# IF the Midpoint value within the list is equal to Target value: return true
if List[Midpoint] == Target:
return True
else:
if List[Midpoint] < Target:
return Algorithms.SearchAlgorithm.RecursiveBinarySearch.RecursiveBinarySearchAlgo(List[Midpoint + 1:], Target)
else:
return Algorithms.SearchAlgorithm.RecursiveBinarySearch.RecursiveBinarySearchAlgo(List[:Midpoint], Target)    
def Verify(Result):
print("Target found: ", Result)
Numbers = [1,2,3,4,5,6,7,8]
# Test Cases 1
Result = RecursiveBinarySearchAlgo(Numbers, 12)
Verify(Result)

# Test Case 2
Result = RecursiveBinarySearchAlgo(Numbers, 5)
Verify(Result)

我得到一个名称错误,类没有定义name 'Algorithms' is not defined

我试着用self进入每个类的init函数。但是我仍然得到相同的错误

程序应该产生:

Target found : True
target found: False

我不知道为什么在内部类中我们不能直接调用方法,而是作为@juanpa。arrivillaga建议再上一级,除了在方法上使用self关键字外,还使用内部类作品的实例:

class Algorithms:
class SearchAlgorithms:
class RecursiveBinarySearch:
def RecursiveBinarySearchAlgo(self, List, Target):
'''
Return true value if it exists and a false if it doesn't
'''
# If the length of the List is equal to 0 : return False
if len(List) == 0:
return False
# Else if the list is not empty
else:
Midpoint = (len(List))//2
# IF the Midpoint value within the list is equal to Target value: return true
if List[Midpoint] == Target:
return True
else:
if List[Midpoint] < Target:
return self.RecursiveBinarySearchAlgo(List[Midpoint + 1:], Target)
else:
return self.RecursiveBinarySearchAlgo(List[:Midpoint], Target)
def Verify(self, Result):
print("Target found: ", Result)
rbs = RecursiveBinarySearch()
Numbers = [1,2,3,4,5,6,7,8]
# Test Cases 1
Result = rbs.RecursiveBinarySearchAlgo(Numbers, 12)
rbs.Verify(Result)
# Test Case 2
Result = rbs.RecursiveBinarySearchAlgo(Numbers, 5)
rbs.Verify(Result)

如果你不想在这种情况下创建实例,你也可以使用@classmethod注释:

class Algorithms:
class SearchAlgorithms:
class RecursiveBinarySearch:
@classmethod
def RecursiveBinarySearchAlgo(cls, List, Target):
'''
Return true value if it exists and a false if it doesn't
'''
# If the length of the List is equal to 0 : return False
if len(List) == 0:
return False
# Else if the list is not empty
else:
Midpoint = (len(List))//2
# IF the Midpoint value within the list is equal to Target value: return true
if List[Midpoint] == Target:
return True
else:
if List[Midpoint] < Target:
return cls.RecursiveBinarySearchAlgo(List[Midpoint + 1:], Target)
else:
return cls.RecursiveBinarySearchAlgo(List[:Midpoint], Target)
def Verify(Result):
print("Target found: ", Result)
Numbers = [1,2,3,4,5,6,7,8]
# Test Cases 1
Result = RecursiveBinarySearch.RecursiveBinarySearchAlgo(Numbers, 12)
RecursiveBinarySearch.Verify(Result)
# Test Case 2
Result = RecursiveBinarySearch.RecursiveBinarySearchAlgo(Numbers, 5)
RecursiveBinarySearch.Verify(Result)

最新更新