Super#

So far we’ve been repeating a lot of the parent class constructor in the child class. Let’s learn how to fix it!

super()#

The super() function in Python is used to access methods in a parent class from a child class. This is useful when you override a method from the parent class or want access to the original functionality.

The way super() works is by creating a version of the current instance with only the methods of the parent class.

Example

In the example below, a FeeChargingAccount overrides the parent class withdraw method and then calls the parent class withdraw method with an additional fee amount. See line 8.

class BaseAccount:
    def __init__(self, bsb_number, account_number, balance, holder_name):
        self.bsb_number = bsb_number
        self.account_number = account_number
        self.balance = balance
        self.holder_name = holder_name

    def withdraw(self, amount):
        self.balance += amount


class FeeChargingAccount(BaseAccount):
    def withdraw(self, amount):
        fee = 2.50
        super().withdraw(amount + fee)  # Calls the method defined on line 8

Using super() in the Constructor#

We can use super() to avoid repeating initialising attributes in child classes.

Example

In the example below, we have successfully avoided repeating the parent class constructor code on line 13.

class BaseAccount:
    def __init__(self, bsb_number, account_number, balance, holder_name):
        self.bsb_number = bsb_number
        self.account_number = account_number
        self.balance = balance
        self.holder_name = holder_name

    def deposit(self, amount):
        self.balance += amount


class SavingsAccount(BaseAccount):
    def __init__(self, bsb_number, account_number, balance, holder_name, interest_rate):
        super().__init__(bsb_number, account_number, balance, holder_name)
        self.interest_rate = interest_rate
Code Challenge: Attack Ship

Note

For this exercise you’ll need the solution to Inheritance Example > “Bombs Away!”

Now that we’ve tested that the AttackShip it’s time to add real torpedo rounds.

../../_images/attack_ship.png

Modify your AttackShip class method so that:

  • a rounds attribute used to track the number of rounds on board the ship, which is set in the constructor

  • a fire_round method which shoots off a single round if there is one available by decreasing rounds by 1.

AttackShip Specifications

  • Inherit from the provided Spaceship class

  • Additional attributes:

    • rounds the number of rounds remaining on the ship

  • __init__ method with

    • Parameters:

      • self

      • name

      • fuel_capacity

      • rounds

    • Actions:

      • Call the Spaceship constructor to set common attributes with the same name

      • Set the rounds attribute

  • fire_round method with

    • Parameters:

      • self

    • Actions:

      • Reduces the remaining rounds by 1.

    • Returns:

      • None (no return statement required)

Here is some code for you to start with:

class Spaceship:
    def __init__(self, name, fuel_capacity):
        self.name = name
        self.fuel_weight = 0
        self.fuel_capacity = fuel_capacity

    def refuel(self, weight):
        if self.fuel_weight + weight > self.fuel_capacity:
            self.fuel_weight = self.fuel_capacity
        else:
            self.fuel_weight += weight

    def fire_laser(self):
        if self.fuel_weight >= 10:
            self.fuel_weight -= 10


# ADD YOUR CLASS HERE


# X-Wings carry 6 torpedos
xwing = AttackShip("X Wing", 500, 6)
xwing.refuel(500)  # Fill 'er up!

xwing.fire_round()
print(xwing.rounds)
Solution

Solution is locked