[Python] Class instantiation in python multiple inheritance and passing arguments [closed]

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying something like below. Need some help with this design as it is not working out...

    Have two parent classes "Organiser" and "Activity". Class Event is a child class with two parent classes "Organiser" and "Activity". Event() has additional arguments besides inheriting Parent's arguments.

    what I don't want to do is that when I instantiate object of class Event, I don't want to provide all arguments of its own as well as for its parents in one go.

    Instead, I want to intake only arguments for Event() and then call Parent Class methods to intake their arguments.

    What i intent to achieve is let the respective parent class deal with its inputs, validations etc and not burden child class with all of it.

    class Organiser:
    def __init__(self, name):
    self.name = name

    def addOrganiser(self):
    self.name = input("provide name of organiser: ")

    class Activity:
    def __init__(self, type):
    self.type = type

    def addActivity(self):
    self.type = input("provide type of activity: ")

    class Event(Organiser, Activity): #two parents
    def __init__(self, eventName, venue):
    self.eventName = eventName
    self.venue = venue

    def createEvent(self):
    ....
    ....

    #---- main program

    Event1 = Event("Greenwood Challenge", "Arthur Ground")
    Event1.createEvent()
    Event1.addOrganiser()
    Event1.addActivity()

    Continue reading...

Compartilhe esta Página