Camel Driver Training for Desert Navigation

PythonPythonBeginner
Practice Now

Introduction

Imagine a scenario in the vast and scorching Arabian desert where a group of nomads relies on their trusty camels for transportation. In this setting, you will take on the role of a camel driver tasked with training new camels to inherit the navigation and endurance skills essential for desert travel.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python/ObjectOrientedProgrammingGroup -.-> python/inheritance("`Inheritance`") subgraph Lab Skills python/inheritance -.-> lab-271562{{"`Camel Driver Training for Desert Navigation`"}} end

Understanding Inheritance

In this step, you will create a Python class that represents a basic camel. You will then create a new class for a racing camel, inheriting the traits and abilities of the basic camel and adding additional speed attributes.

In /home/labex/project/camel.py:

## Create a basic Camel class
class Camel:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def navigate(self):
        print(f"{self.name} is navigating through the desert.")

## Create a RacingCamel class that inherits from Camel
class RacingCamel(Camel):
    def __init__(self, name, age, speed):
        super().__init__(name, age)
        self.speed = speed

    def race(self):
        print(f"{self.name} is racing at {self.speed} mph.")

## Create a basic Camel instance
basic_camel = Camel("Tom", 5)
basic_camel.navigate()

Run the script:

python camel.py

The information below should be displayed on your terminal:

Tom is navigating through the desert.

Using Super() Function

In this step, you will enhance the RacingCamel class by using the super() function to call the parent class' navigate() method.

Update and add the following code in the /home/labex/project/camel.py file:

class RacingCamel(Camel):
    def __init__(self, name, age, speed):
        super().__init__(name, age)
        self.speed = speed

    def race(self):
        print(f"{self.name} is racing at {self.speed} mph.")

    def navigate(self):
        super().navigate()
        print(f"{self.name} is navigating at high speed.")

## Create a RacingCamel instance
racing_camel = RacingCamel("Sprint", 8, 60)
racing_camel.navigate()
racing_camel.race()

Run the script:

python camel.py

The information below should be displayed on your terminal:

Sprint is navigating through the desert.
Sprint is navigating at high speed.
Sprint is racing at 60 mph.

Summary

In this lab, you learned the concept of inheritance in Python. By defining parent and child classes, you can create a hierarchy of classes that inherit attributes and methods, thereby promoting reusability and maintaining a clear structure in your code. Understanding inheritance is essential in object-oriented programming and allows for efficient code organization and development.

Other Python Tutorials you may like