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

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to