2014-12-02 74 views
0

我只是好奇在這個特殊的情況下使用沖洗是什麼,因爲它對我的應用程序是否起作用沒有影響。有問題的線在第二類Route定義下,即「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

沖洗保證輸出是立即寫到標準輸出,而如果你不那麼衝它威力(可能)在未指定的時間內在緩衝區中停留。

字符串是否保留取決於輸出設備。某些輸出設備是行緩衝的,所以帶有換行符的print將自動刷新緩衝區。但這通常不是真實的,所以flush可以確保它不管寫出來。

對於您的具體情況,是否有用取決於您是否在意打印可能不會立即生效的可能性。沒有明確的清除,在最壞的情況下,輸出可能不會出現,直到程序終止。如果您的程序將立即終止而不需要用戶交互,那麼沖洗不會有任何區別。

+0

好的,這對我有意義。對於比我的規模更大的項目來說,它似乎是一個更有用的功能。也許對於某種遊戲來說,時間滯後可能會成爲問題。或者可能是像亞馬遜或甚至YouTube這樣的更大的規模,如果某些事情需要超過一毫秒的時間,人們可能會變得胡思亂想。非常感謝你@Rufflewind我很欣賞那樣的詳細答案。另外,謝謝你的imareaver。 – 2014-12-02 01:02:56

相關問題