Advanced Python 3 Code Challenges Classes 1.Robot Inheritance

Hello,
In this exercise we are asked to add 2 new classes that inherit from another class. Both DriveBot and WalkBot inherit from Robot.
We are asked to use superclass constructor and in each of the new classes we need to pass 1 new parameter : In the class DriveBot, motor_speed and in WalkBot , ‘steps_per_minute’ as the parameter for speed in the super class.
In the explanation for the answer provided 'motor_speed´ and ‘steps-per_motor’ are put in place of ‘speed´’. Here answered provided:

**class DriveBot(Robot):**
**    def __init__(self, motor_speed = 0, direction = 180, sensor_range = 10):**
**        super().__init__(motor_speed, direction, sensor_range)**
** **
**class WalkBot(Robot):**
**    def __init__(self, steps_per_minute = 0, direction = 180, sensor_range = 10, step_length = 5):**
**        super().__init__(steps_per_minute, direction, sensor_range)**
**        self.step_length = step_length*

I need help confirming how does the interpreter knows that these new parameters will occupy the value of ‘speed’ in Robot as ‘init’ in this class as there is no relation declared that associates new parameter name to the old parameter name.
I am assuming that it is because of the position. that Robot will assume any value that comes in that place holder position as ‘speed’. But I would like reassurance or to be corrected in case I am mistaken.

Here ‘init’ from Robot:


**def __init__(self, speed = 0, direction = 180, sensor_range = 10):**

**        self.speed = speed**

**        self.direction = direction**

**        self.sensor_range = sensor_range**

**        self.obstacle_found = False**

**        Robot.robot_count += 1**

**        self.id = Robot.robot_count**

Here link to the excercise:
https://www.codecademy.com/courses/learn-python-3/articles/advanced-python-code-challenges-classes

For reference:
I am currently around 23% progress for the Data Analyst Path and about 93% progress for the Learn Python 3 Course.
I am located in the Dom. Rep.

1 Like

If possible could you either edit or repost your code but formatted for the forums- How do I format code in my posts?
It helps a lot for anyone viewing it :slightly_smiling_face:

Thank you for the observation. I thing was able to edit it.

1 Like

Calling super saves you from something like hard coding Robot.__init__() into your new __init__ method in your subclass (amongst other things). You’re still relying on the method defined in the original class so the binding of attributes .speed etc. behave as they did here.

In your new __init__ method you have this section of code-

super().__init__(steps_per_minute, ...

So you’re passing the steps_per_minute to this function under the parameter name speed. So yes, it’s position based as those are your positional arguments to that function.

You’re passing arguments like this effectively-

speed=steps_per_minute, direction=direction, sensor_range=sensor_range)
4 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.