I have a bot that connects to a server. The server code is a base class and the code that processes the data from the server is in a class which inherits the base class.
I have two conditions for this assignment:
- The entire code should be runnable from one line, which in my code, is line 56:
Racing1 = Racing()
- The base class attributes can only be initialized once because of a special command (omitted here) on line 11
My question is:
How can I reference the 'self.is_race_started' attribute from the base/Racing class from the extended/RacingData class?
My attempt is on line 52, but I am getting a "AttributeError: 'RacingData' object has no attribute 'is_race_started'"
I am new to OOP -- any help would be greatly appreciated!
#!/usr/bin/env python class Racing(object): WELCOME = 'Welcome to Racing' def __init__ (self): # this can only be initialized once self.is_race_started = True # here is a variable which can only be initialized once (registers a command with the server) # while bot.is_connected_to_server: if self.is_race_started: start_timestamp = 2.0 # get timestamp at start finish_timestamp = 5.0 # get timestamp at finish self.is_race_started = False # To start a new race: self.reset_variables() # Use the extended class to process the data rr = RacingData() rr.set_data( start_timestamp , finish_timestamp ) rr.print_results() # Try printing out the finish time print '\nThe finish time of the last race was: %f' % rr.finish_time def reset_variables(self): self.is_race_started = True class RacingData(Racing): def __init__ (self): #super(RacingData, self).__init__() # what does this do? self.number_of_racers = 0 self.WELCOME2 = 'Calculating your finish time...' def set_data( self , start_timestamp , finish_timestamp ): self.start_timestamp , self.finish_timestamp = start_timestamp , finish_timestamp self.number_of_racers += 1 def calculate_time(self): self.finish_time = self.finish_timestamp - self.start_timestamp # save time so it can be referenced later return self.finish_time def print_results(self): print Racing.WELCOME print self.WELCOME2 #print 'Has the race started? %s' % self.is_race_started print '\nYou finished the race in %f! You are the #%d person to finish this race.' % ( self.calculate_time() , self.number_of_racers ) if __name__ == '__main__': Racing1 = Racing() # the requirements of this program is that everything must be runnable with one line