class Example(object):
def doSomething(self, num):
if(num < 10 ) :
//print the number
else :
//call doSomething() again
在这里,如何在方法内的doSomething
条件中调用else
方法?
发布于 2016-07-01 15:38:19
使用self.doSomething(num-1)
调用它,因为doSomething
引用的是全局函数,而不是类中的函数。还将print
放在if
之前,这样它就可以打印数字,而不管它是什么(因此您可以看到数字在减少),并在其中放置一个return
:
class Example(object):
def doSomething(self, num):
print num
if(num < 10 ) :
return
else :
self.doSomething(num-1)
>>> x = Example()
>>> x.doSomething(15)
15
14
13
12
11
10
9
>>>
https://stackoverflow.com/questions/38149141
复制相似问题