mercredi 12 août 2020

How to deal with calling a method which is producing object instances?

The thing I'm struggling with is how to create a method, which by a "for loop", be automatically producing CarObject class instances, filling particular parameters based on data from "get" methods.

I've implemented something like this below. Seems fine until it comes to calling create_object() method by a class instance outside of the class, when I need to fill out parameters from the constructor... I'm guessing my problem is about designing patterns, which I haven't implemented properly, but nothing comes to my mind how could I reconstruct the code to reach my desirable output.


all of the get methods are implemented above CarObject class

class CarObject(object):
    def __init__(self, make, model, mileage, year, engine, engine_type):
        self.make = make
        self.model = model
        self.mileage = mileage
        self.year = year
        self.engine = engine
        self.engine_type = engine_type

    def create_object(self):
        entry = [{'MAKE': self.make, 'MODEL': self.model, 'MILEAGE': self.mileage, 'YEAR': self.year,
              'ENGINE': self.engine, 'ENGINE_TYPE': self.engine_type}]

        for num, x in enumerate(range(len(get_year()))):
            self.mileage = get_mileage()[num]
            self.year = get_year()[num]
            self.engine = get_engine_capacity()[num]
            self.engine_type = get_fuel_type()[num]

            c1 = CarObject('UNDEFINED', 'UNDEFINED', self.mileage, self.year, self.engine, self.engine_type)
            entry.append(c1)

        return entry


o1 = CarObject()  # And here I need to fill these parameters, while the thing I only want is to print the outcome produced by create_object() method...
print(o1)

Aucun commentaire:

Enregistrer un commentaire