0

I would like to access the instance variable (orders) of a class (DailyOrders) from the init of another, keeping in mind that the class containing the instance variable is a parent to the other. This seems to perfect use for inheritance but I couldn't get it to work. Here is the code.

class DailyOrders():

    PRICE_PER_DOZEN = 6.5

    def __init__(self, day):
        self.orders = []
        self.day = day

    def total_eggs(self):
        total_eggs = 0
        for order in self.orders:
            total_eggs += order.eggs
        return total_eggs

    def show_report(self):
        if self.total_eggs() < 0:
            print("No Orders")
        else:
            print(f"Summary:\nTotal Eggs Ordered: {self.total_eggs()}\n*********")
            print(f"Average Eggs Per Customer: {self.total_eggs() / len(self.orders):.0f}")


class EggOrder():

    def __init__(self, eggs=0, name=""):
        if not name:
            self.new_order()
        else:
            self.name = name
            self.eggs = eggs

        orders.append(self)

    def new_order(self):
        self.name = string_checker("Name: ")
        self.eggs = num_checker("Number of Eggs: ")

    def get_dozens(self):
        if self.eggs % 12 != 0:
            dozens = int(math.ceil(self.eggs / 12))
        else:
            dozens = self.eggs / 12
        return dozens

    def show_order(self):
        print(f"{self.name} ordered {self.eggs} eggs. The price is ${self.get_dozens() * DailyOrders.PRICE_PER_DOZEN}.")


if __name__ == "__main__":

    friday = DailyOrders("Friday")
    friday_order = EggOrder(12, "Someone")
    friday_order.show_order()
    friday.show_report()

    saturday = DailyOrders("Saturday")
    saturday_order = EggOrder(19, "Something")
    saturday_order.show_order()
    saturday.show_report()

I have not tried using inheritance before, but one proposed solution is to use super(EggOrder, self).__init__() but this made me provide the day, as it was going to create another instance of the DailyOrders class. I don't want this.

1
  • Please fix your indentation. Commented Feb 14, 2019 at 5:01

1 Answer 1

3

Just inherit and user super to call the parent initializer:

class DailyOrders:

    def __init__(self, day):
        self.orders = []
        # ...

class EggOrder(DailyOrders):

    def __init__(self, day, eggs=0, name=""):
        super().__init__(day)
        # Now self.orders is available.

Keep in mind that if the parent initializer receives any parameter, the child must receive it as well in order to be able to pass it.

Not providing a day param ...

If you don't want to provide a day param you should have another class with the interface/functionality that's common to the others, and the inherit from such base class:

class BaseOrders:
    def __init__(self):
        self.orders = []
        # ...

class DailyOrders(BaseOrders):

    def __init__(self, day):
        super().__init__()
        # Now self.orders is available.
        self.day = day
        # ...

class EggOrder(BaseOrders):

    def __init__(self, eggs=0, name=""):
        super().__init__()
        # Now self.orders is available.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.