views:

226

answers:

2

i need to write code for two "robots" that will chase each other around the screen ive managed to do most of it but what i need help with is the getName() command which returns the name of the robot (as a string) which will be overridden in the subclass.

can anyone help as im new to python and havnt done this before

+2  A: 
 #Inside robot class   
 def __str__(self):
       return self.name;
Tom
cheers for the help tom
matt
+8  A: 

Given the specs you describe for your homework (there must be a getName() command which returns the name of the robot (as a string) which will be overridden in the subclass) the existing answer showing a special method __str__ is unlikely to help -- they are pretty weird and unPythonic specs, but, hey, homework is homework, after all, not real life.

So I imagine they want you to code in the base class something like:

class RobotBase(object):
    def getName(self):
        return "GenericRobot"
    # and other methods &c

and in each subclass have a different override of this, such as

class RobotShiny(RobotBase):
    def getName(self):
        return "ShinyRobot"

class RobotShady(RobotBase):
    def getName(self):
        return "ShadowRobot"

and so forth/

Alex Martelli