python - what is the use of flushing in this instance? -


i curious use of flushing in particular instance, has no bearing on whether app functions or not. line in question under 2nd class route definition, "sys.stdout.flush()" line. used for?

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         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 1 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 average of 3 ==========================      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 3 , divides 3         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 3 ==========================      def calc_total(self): #calculates total of 3         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 3 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 0         self.stop_two = ""         self.stop_two_mileage = "" #makes sure mileage isn't 0         self.stop_three = ""         self.stop_three_mileage = "" #makes sure mileage isn't 0  #===================== getter/setter ==========================          @property #stop_one_mileage getter         def stop_one_mileage(self):             return self.__stop_one_mileage #returns 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 10 (nice round number)             else:                 self.__stop_one_mileage = m #returns value long input more 0 

flush guarantees output written standard output immediately, whereas if don't flush might (potentially) stick around in buffer unspecified amount of time.

exactly whether string stay depends on output device. output devices line-buffered, print newline automatically flush buffer. not true in general, flush ensures it's written out regardless.

for specific case, whether it's useful depends on whether care possibility print may not take effect immediately. without explicit flush, in worst case scenario, output may not appear until program terminates. if program terminate without requiring user interaction flush not make difference.


Comments

Popular posts from this blog

javascript - Wordpress slider, not displayed 100% width -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -