0

I am totally new to python there is something i can't understand in this code:

class Automobile():
    numberOfWheels = 2
    enginePower = 0
    def __init__(self,**kwargs):
        for i in kwargs:
            self.i=kwargs[i]

1-this initializer suppose to create an instance attribute for each keyword argument passed to the Automobile() constructor? or i am missing something here?

2-if the above is correct ,testing the class give otherwise result:

>>>car=Automobile(numberOfWheels = 4, enginePower = 150)
>>>car.enginePower
0

as you can see the car object still refer to the Automobile class attribute what is going on?

Alan Omar
  • 4,023
  • 1
  • 9
  • 20
  • 1
    `self.i` is not related to `i`, it just means you want to set the attribute named `i`. You want to either use `setattr(self, i, kwargs[i])`, or, just use `vars(self).update(kwargs)` in one step, no loop required. – Martijn Pieters Jan 28 '19 at 21:11

1 Answers1

0

You need to use setattr to set attributes

Benoît P
  • 3,179
  • 13
  • 31