我从教程中创建了这段代码,但很难理解一个关键概念:“next”如何变得等于字符串“s”?self.start 如何引用该字符串?
import sys
class DemoClass(object):
def __init__(self, start): # executes once!
self.start = start
print "_init_"
def play(self): # executes once!
next = self.start
print "next"
while True:
attr = getattr(self, next) #get attribute of the class
next = attr()
def s(self): # these methods start calling each other once executed
print "s"
return 't'
def t(self):
print "t"
i = raw_input(">")
if i == 's': # if user input 'i' is equal to 's', call func "s"
return "s"
elif i == 'v':
return "v"
else:
exit(0)
def v(self):
print "v"
return 's'
a = DemoClass("s")
a.play()