我很好奇在这个特定的实例中使用刷新是什么,因为它与我的应用程序是否起作用无关。有问题的行在第二类路由定义下,即“sys.stdout.flush()”行。那将用于什么?
class Route(object):
def __init__(self):
self.__route = [] #route information array
def add_route(self, t): #user generated input array
self.__route.append(t)
print t.name
sys.stdout.flush() #using "flush" method
def compile_list(self): #route list compiler
output = "" #sets addition variable
for route in self.__route: #looping inputs
output += "<div id='container'><div class='results-container'><span class='title'>Route name: </span>" + "<span class='result'>" + route.name + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop One: </span>" + "<span class='result'>" + route.stop_one + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop One Mileage: </span>" + "<span class='results-container'>" + route.stop_one_mileage + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop Two: </span>" + "<span class='results-container'>" + route.stop_two + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop Two: </span>" + "<span class='results-container'>" + route.stop_two_mileage + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop Three: </span>" + "<span class='results-container'>" + route.stop_three + "</span></div><br />" + "<div class='results-container'><span class='title'>Stop Three: </span>" + "<span class='results-container'>" + route.stop_three_mileage + "</span></div><br />"
return output #output return
#===================== Calculates the average of all three ==========================
def calc_average(self): #average calculation
stop_one_mileage = self.__route[0].stop_one_mileage #stop_one_mileage average
stop_two_mileage = self.__route[0].stop_two_mileage #stop_two_mileage
stop_three_mileage = self.__route[0].stop_three_mileage #stop_three_mileage
avg = (int(stop_one_mileage) + int(stop_two_mileage) + int(stop_three_mileage))/3 #adds all three and divides by three
return "<div class='results-container'><span class='title'>Average Mileage: </span><span class='results-container'>" + str(avg) + " miles</span></div>" #returns results
#===================== Calculates total of all three ==========================
def calc_total(self): #calculates the total of all three
stop_one_mileage = self.__route[0].stop_one_mileage #stop_one_mileage
stop_two_mileage = self.__route[0].stop_two_mileage #stop_two_mileage
stop_three_mileage = self.__route[0].stop_three_mileage #stop_three_mileage
total = int(stop_one_mileage) + int(stop_two_mileage) + int(stop_three_mileage) #sets total of all three input
return "<div class='results-container'><span class='title'>Total Mileage: </span><span class='results-container'>" + str(total) + " miles</span></div>" + "</div>" #returns results
class FormData(object): #form data object
def __init__(self):
self.name = ""
self.stop_one = ""
self.__stop_one_mileage = "" #makes sure mileage isn't zero
self.stop_two = ""
self.stop_two_mileage = "" #makes sure mileage isn't zero
self.stop_three = ""
self.stop_three_mileage = "" #makes sure mileage isn't zero
#===================== Getter/Setter ==========================
@property #stop_one_mileage getter
def stop_one_mileage(self):
return self.__stop_one_mileage #returns the stop_one_mileage
@stop_one_mileage.setter #stop_one_mileage setter
def stop_one_mileage(self, m):
if m <= 0:
self.__stop_one_mileage = 10 #mileage set to 10 (nice round number)
else:
self.__stop_one_mileage = m #returns a value as long as the input is more than zero
答案 0 :(得分:0)
强制将完整的I / O缓冲区写入stdout。在奇数竞争条件(缓冲区写入的确切时间很重要)或过早终止(没有刷新,缓冲区中的所有信息都将丢失)等情况下,这可能很重要。
更多信息
答案 1 :(得分:0)
刷新保证输出将立即写入标准输出,而如果您不冲洗则可能(可能)留在缓冲时间不确定。
字符串是否保留的确切取决于输出设备。某些输出设备是行缓冲的,因此带有换行符的print
将自动刷新缓冲区。但总的来说这并不正确,所以同花顺确保无论如何写出来。
对于您的具体情况,它是否有用取决于您是否关心打印可能不会立即生效。如果没有显式刷新,在最坏的情况下,输出可能不会出现,直到程序终止。如果你的程序会立即终止而不需要用户交互,那么刷新就没有任何区别。