New Versus Init
Talk it through out loud with your AI coach.
The problem
- medium
- Pro
You want a subclass of tuple called Point that always rounds its two coordinates to whole numbers, but rounding them in the init method has no effect. Explain the difference between the new and init special methods, and how you'd fix the Point class.
What a strong answer covers
- The separate jobs of creating an object and initialising it.
- Why immutable types change which of the two you must override.
- How the corrected class hands values on to its parent.
- Another situation where controlling creation is useful.
Common mistake: Trying to assign to the tuple's items inside the initialiser, which fails because the tuple is already built.
Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.
Practise it out loud