Zah*_*hra 1 python recursion static
将递归函数实现为静态方法的正确方法是什么?
这是我如何让它在自动取款机上工作的。我想知道是否有一种“更好”的方法来实现这一点,从而留下更干净的内存占用,看起来更Pythonic,等等。
class MyClass(object):
@staticmethod
def recursFun(input):
# termination condition
sth = MyClass().recursFun(subinput)
# do sth
return sth
Run Code Online (Sandbox Code Playgroud)
您不需要类的实例来执行正确的名称查找;课程本身就可以了。
class MyClass(object):
@staticmethod
def recursive_function(input):
# ...
sth = MyClass.recursive_function(subinput)
# ...
return sth
Run Code Online (Sandbox Code Playgroud)
限定名称是必要的,因为当您执行名称查找时,该名称recursive_function将不在范围内;只会MyClass.recursive_function。