Member Avatar for cyon

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:

  1. The entire code should be runnable from one line, which in my code, is line 56:
    Racing1 = Racing()
  2. 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!

-cyon

#!/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

Recommended Answers

All 2 Replies

You are trying to inherit an instance attribute,
but you need a class attribute.

This code below fixes your problem,
but your code structure is strange,
and I think you have a wrong concept about inheritance.
Also, I fixed the indentation for you, don't use tabs, use spaces.

#!/usr/bin/env python

class Racing(object):
    
    WELCOME = 'Welcome to Racing'
    is_race_started = False
    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?
                #This class is a subclass of Racing
                #So that line will call Racing's __init__ method
                #which is a bad idea since it's recursive, see
                #the line that contians rr=RacingData()
                #I don't think you are supposed to use subclasses
                #in this way
            
        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
Member Avatar for cyon

You are trying to inherit an instance attribute,
but you need a class attribute.

Thanks for your help, it works now!

#!/usr/bin/env python

class Racing(object):
	
	WELCOME = 'Welcome to Racing'
	is_race_started = True
	
	def __init__ (self): # this can only be initialized once

		# here is a variable which can only be initialized once (registers a command with the server)

		# while bot.is_connected_to_server:
		if Racing.is_race_started:
			start_timestamp = 2.0 # get timestamp at start
			finish_timestamp = 5.0 # get timestamp at finish
			Racing.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):
		Racing.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' % Racing.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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.