Home > database >  create a function that returns an object
create a function that returns an object

Time:02-26

I need guidance to create a function that returns an instantiated object of class Car.

It must have the following attributes:

-model (a string data type value)

-kilometers (An integer data type value, which must be initialized to zero)

-Color (A string data type value)

And must have the following method:

-AddKilometers (this method must add one to the kilometers attribute and must return that value).

It receives two arguments:

-model: Data to be assigned to the 'model' attribute of the Car class object. -color: Data to be assigned to the 'Color' attribute of the Car class object.

This is my progress: (I'm a python learner, so you'll see that my code has quite a few mistakes)

def ClassCar(model, color):
    
    class Cars:
        def __init__(self):
            self.kilometres = 0
            self.model= model
            self.color = color 
        
        def AddKilometres (self):
            self.kilometres  = 1
            return self.kilometres
    
    return Cars(model,color)

example of how to execute the function:

c1 = ClassCar('ford','black')
c1.AddKilometres() -> 1
c1.AddKilometres() -> 2
c1.AddKilometres() -> 3

my problem is that when I run the code nothing happens What am I doing wrong?

CodePudding user response:

You have to add the properties you need as parameters to the __init__() method.

def ClassCar(model_name, color_name):  # changed variable names to avoid confusion

    class Cars:
        def __init__(self, model, color):  # add parameters here
            self.kilometres = 0
            self.model = model
            self.color = color

        def AddKilometres(self):
            self.kilometres  = 1
            return self.kilometres

    return Cars(model_name, color_name)


c1 = ClassCar('ford', 'black')
print(c1.AddKilometres())
print(c1.AddKilometres())
print(c1.AddKilometres())

BTW, you don't have to write your own function for this. By default, the __init__() method runs when you instantiate a class, and it would return an object. So, you could just do this:

class Cars:
    def __init__(self, model, color):  # add parameters here
        self.kilometres = 0
        self.model = model
        self.color = color

    def AddKilometres(self):
        self.kilometres  = 1
        return self.kilometres


c1 = Cars('ford', 'black')
print(c1.AddKilometres())
print(c1.AddKilometres())
print(c1.AddKilometres())

The latter is in fact better practice, and the recommended way to do this AFAIK.

CodePudding user response:

You can try this:

class Car():
   def __init__(self, model, color):
     self.model = model
     self.color = color
     self.kilometres = 0

   def AddKilometers(self):
     self.kilometers  = 1
     return self.kilometres

And call like this:

car = Car('ford','black')
print(car.AddKilometres()) # velocity = 1
print(car.AddKilometres()) # velocity = 2
print(car.AddKilometres()) # velocity = 3

CodePudding user response:

You seem to be a little confused about how best to write classes and functions. Let's rework your code:

class Car:
    def __init__(self, model, colour):
        self.model = model
        self.colour = colour
        self.km = 0
    def add_km(self):
        self.km  = 1
        return self.km
    def __repr__(self):
        return f'Model={self.model}, colour={self.colour}, km={self.km}'

car = Car('Ford', 'black')

print(car)
print(car.add_km())
print(car)

Output:

Model=Ford, colour=black, km=0
1
Model=Ford, colour=black, km=1
  • Related