Skip to main content

Section 13.7 Improving our Constructor

Our constructor so far can only create 1 by 1 rectangles. To create a rectangle with width 7 and height 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.
Now when we create new points, we supply the width and height as parameters. When the rectangle is created, the values of init_width and init_height are assigned to the state of the object.