19.5. Improving our Constructor

Our constructor so far can only create points at location (0,0). To create a point at position (7, 6) requires that we provide some additional capability for the user to pass information to the constructor. Since constructors are simply specially named functions, we can use parameters (as we’ve seen before) to provide the specific information.

We can make our class constructor more general by putting extra parameters into the __init__ method, as shown in this codelens example.

Activity: CodeLens 19.5.1 (chp13_improveconstructor)

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

Now when we create new points, we supply the x and y coordinates as parameters. When the point is created, the values of initX and initY are assigned to the state of the object.

Simple object has state and methods
You have attempted 1 of 2 activities on this page