0
from multipledispatch import dispatch

class Vehicle:
    def __init__(self, ownerID, category, power, make, model, year):
        self.ownerID = self.personID
        self.category = category
        # Ask if we need to enforce "N", "G", "D", "H", "E", "O"
        self.power = power
        self.make = make
        self.model = model
        self.year = int(year)


class Car(Vehicle):
    def __init__ (self, make, model, year, vin, stateReg, plate, odometer, carType, axles):
        #if self.make != None and self.model != None and self.year != None:
            self.vin = vin
            #Do we have to enforce 2 characters in statereg? use if statement here to enforce them
            self.stateReg = str(stateReg)
            self.plate = plate
            self.odometer = odometer
            self.cartype = carType
            self.axles = axles

        
class Person:
    @dispatch(int, str, str)
    def __init__(self,personID, lastName, firstName):
        self.personID = personID
        self.lastName = str(lastName)
        self.firstName = str(firstName)
    
    @dispatch(int,str,str,str,str,int,int)
    def __init__(self, personID, lastName, firstName, address, city, state, zipcode, telephone):
        self.personID = personID
        self.lastName = lastName
        self.firstName = firstName
        self.address = address
        self.city = city
        self.state = state
        self.zipcode = zipcode
        self.telephone = telephone

        
def main():
     personid1 = Person(1, "Dwarf", "Grumpy", "1 East North Drive", 'Utopia', 'NY', 99991-1492, None)
     personid2 = Person(2, "White", "Snow", "14 Empty Tree Lane", 'Enchanted Forest', 'NY', 99992-1498, None)
     personid3 = Person(3, "Witch", "Wicked", "Unknown", None, None,None, 555-555-1234)
     personid4 = Person(4, "Charming", "Prince", "1 Majestic Palace", 'NY', 'NY', 99997, None) 
    
     p1 = (1, "ln", "fn", "sd","sd",21, 13)
    
     car1 = Car('Chevy', 'Chevelle', 1971, None,'NY','#POIZNAPPL', 0, 'COU', 2)
     car1.ownerID = 1
     car2 = Car(1967, "Ford Shelby" , 'NY' ,'#EVELYN', "Prince Charming",47,281, 'COU', 2)
     car3 = Car(2021 , )
     car4 = Car(1971, "Chevy Chevelle" , 'NY' ,'#POIZNAPPL', 0000 , 'COU', 2)
    
   
     listpc =(p1, car1, car2, car3, car4)
     print(listpc)
    

if __name__ == '__main__':
    main()

I get an error saying that the second constructor in class Person is not used. The error is:

NotImplementedError: Could not find signature for __init__: <int, str, str, str, str, str, int, NoneType>

Also in the second list of objects for class car it says missing arguments.

ddejohn
  • 8,775
  • 3
  • 17
  • 30
Ahmad
  • 1
  • 1
  • I'm curious why you're using multiple dispatch here when default arguments seems to cover your use case? Furthermore, this doesn't seem to be an appropriate use of multiple dispatch as I understand it -- I'm under the impression that it's usually used to define the same operations for different types of objects so you only need to use one function name, which is really only useful in non-object oriented languages. To me, it looks more like you're trying to overload constructors, which I think is different behavior. – ddejohn Apr 27 '22 at 23:54
  • From what I can see, I think you'd be better off with keyword-only arguments with default values for each. Right now it seems as though what you're trying to do is allow for the instantiation of your classes in different ways (different order of arguments, different numbers of arguments, etc). – ddejohn Apr 28 '22 at 00:00
  • I don't see any constructor overloading for `Car` and you are definitely trying to create instances of `Car` incorrectly. Your `Person` instantiations are also not correct: most of them pass `None` for `telephone` but you haven't built a constructor for that case. That's what the error is telling you. – ddejohn Apr 28 '22 at 00:05

0 Answers0