问题是它第一次在列表中给出1个值,但从下一次开始,它将用新值返回以前的值。 #第一个请求答案:{“number”:[“1”]} #第二个请求答案:{“number”:[“1”,“1”]} #第三个请求答案:{“number”:[“1”,“1”,“1”]} #等等 #为什么新对象的列表会从以前的请求中获取值
import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
class testInput(messages.Message):
number = messages.IntegerField(1)
class testOutput(messages.Message):
number = messages.IntegerField(1, repeated=True)
class counter:
count = []
def add(self, number):
self.count.append(number)
@endpoints.api(name='testClass', version='v1.0')
class testClass(remote.Service):
@endpoints.method(testInput, testOutput,
path='countNow', http_method='GET',
name='countNow')
def countNow(self, request):
#creating a new object of counter class
counterObj = counter() # NOTE: IT SHOULD A NEW INSTANCE AND ITS ALL OBJECT MUST BE NEW
# e.g count list must be empty
#getting the new number from request
requestNumber = int(request.number)
#creating an object of output class
output = testOutput()
#adding the number in list
counterObj.add(requestNumber) # NOTE: ADDING A NUMBER IN THE LIST
#HENCE: LIST SHOULD CONTAIN ONLY ONE VALUE IN IT AND ITS LENGTH MUST BE: 1
#storing the list in output
output.number = counterObj.count
#returning output
return output #RETURNING THE LIST AND IT SHOULD RETURN ONLY SINGLE VALUE IN LIST
application = endpoints.api_server([testClass])
试试这个,构造函数
__init__
需要重新初始化对你来说
这个变量
count
类变量将被所有实例共享相关问题 更多 >
编程相关推荐