0

I'm new to ptyhon I'm trying to do something that should be quite simple but I can't do it.

I have to create objects that will have a different number of attributes each time. For example :

OBJECT A
    Attribute 1
    Attribute 2
    Attribute 3
OBJECT B
    Attribute 1
    Attribute 2

The attribute 1 will always be there (with different values but there will be at least one attribute in the object). I've created an example class :

class car():
    def__init__(self, Color, **kwargs):
        self.Color = Color 

        for key, value in kwargs.iteritems():
            setattr(self, key, value)

So now I have to call the contructor, but as the number of attributes will be variable, how should I do it ?

CAR1_ATTRIBUTES = (Passengers = 5, Tires = 4)
CAR2_ATTRIBUTES = (DriverName = "John", Tires = 4, DoorsNumber = 5)
CAR3_ATTRIBUTES = (DriverName = "Tom", Velocimeter = "Y", DoorsNumber = 4)

CarsList = []
for i in range(0,3):
    CarObject = Car("RED", RESTOFATTRIBUTES)
    CarsList.append(CarObject)

"RESTOFATTRBUTES" will be variable (it could be one single argument or 1563...)

Ralk
  • 443
  • 1
  • 6
  • 17
  • 1
    What type of variable is `RESTOFATTRIBUTES`? – OneCricketeer Apr 14 '17 at 00:40
  • That gets back to why you are doing it in the first place. You can do `Car("RED", horn="honK", lights="flash")` but since I don't know what problem you are trying to solve, I can't really say whether that's right. – tdelaney Apr 14 '17 at 00:44
  • If it's a dictionary, you can use `**RESTOFATTRIBUTES` – OneCricketeer Apr 14 '17 at 00:45
  • 2
    This is a typical XY Problem https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem. You are showing us a solution but not the problem you are trying to solve. – tdelaney Apr 14 '17 at 00:46
  • Why not use a dictionary instead of an unknown number of attributes? – silel Apr 14 '17 at 00:46
  • Imagine that I have this arguments for the first object : [TiresNumber, WindowsNumber] and these others for the second one [TiresNumber, DoorsNumber, PassengersNames] and for the third one... etc. How should I modify this line -CarObject = Car("RED", RESTOFATTRIBUTES)" to create each time in the loop an object with a different number of arguments. – Ralk Apr 14 '17 at 00:48
  • Possible duplicate of [What does \*\* (double star) and \* (star) do for parameters?](http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-parameters) – Azat Ibrakov Apr 14 '17 at 00:48
  • **kwargs can take an infinite amount (not really) of arguments, so you can just add more attributes like @tdelaney showed you. – Andrei Tumbar Apr 14 '17 at 00:57
  • The problem is not with **kwargs. It's about how can I modify the line "CarObject = Car("RED", RESTOFATTRIBUTES)" dinamically to adapt it to the differents attributes that I have for each car. – Ralk Apr 14 '17 at 01:07
  • `Car("RED", **RESTOFATTRIBUTES)`, where `RESTOFATTRIBUTES = {"Passengers": 5, "Tires": 4}` – Ry- Apr 14 '17 at 01:09

1 Answers1

0

You can pass any number of named parameters key = value, for example,

Car("RED", Passengers = 5, Tires = 4)

and they will be seen as the key : value pairs of the dictionary kwargs. Or directly pass the dictionary:

RESTOFATTRIBUTES = {"Passengers": 5, "Tires": 4}
Car("RED", **RESTOFATTRIBUTES)

You don't have to use the ** syntax. You could also have used the * syntax. You define the function def Car(color, *args):. In the body of the funcion, args is then a tuple where the first element corresponds to, say, Passengers, the second corresponds to, say, Tire, etc. You call Car this way:

Car("RED", 5, 4)

or

RESTOFATTRIBUTES = (5, 4)
Car("RED", *RESTOFATTRIBUTES)

There can be more than only two extra attributes, if the body of the function CAR manages it.

This was a duplicate in a way, but there was nothing wrong in providing an answer in the specific context of this question.