Class Issue`

Milt t2 at NetRH.com
Tue Mar 5 17:39:40 EST 2019


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




More information about the Python-list mailing list