osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Class Issue`


The following code gives me unusual results - base on experience with C++.

class Car:
 ?? # carColor = None
 ?? # mileage = None

 ?? def __init__(self, color = None, miles = None):
 ????? self.mileage = miles
 ????? self.carColor = color

 ?? def print(self):
 ????? print(f"Color:?? {self.carColor}")
 ????? print(f"Mileage: {self.mileage}")

myCar = Car('blue', 15000)
myCar.print()

print()

myCar = Car(25000, 'orange')
myCar.print()


When executed the following results:

Color:?? blue
Mileage: 15000

Color:?? 25000
Mileage: orange

It appears that the constructor ignores the assignment statements and 
makes the assignment based on the ordering in the __init__ declaration.
-- 
Regards,

Milt