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

Class Issue`

On Wed, Mar 6, 2019 at 10:01 AM Milt <t2 at netrh.com> wrote:
> The following code gives me unusual results - base on experience with C++.
>     def __init__(self, color = None, miles = None):
>        self.mileage = miles
>        self.carColor = color
> myCar = Car('blue', 15000)
> myCar = Car(25000, 'orange')
> It appears that the constructor ignores the assignment statements and
> makes the assignment based on the ordering in the __init__ declaration.

Based on your experience with C++, what tells the compiler that you
can pass arguments in any order? Would you write multiple constructors
taking different arguments? Because you didn't do that here.

What you CAN do is call the constructor with keyword arguments:

Car(color='blue', miles=15000)
Car(miles=25000, color='orange')

But otherwise, Python (just like C++) will pass the arguments in the
order you wrote them.