Initializing the Object
Understand how to properly initialize objects in Python by using the __init__ method to set required attributes like positions. Learn why defining constructors prevents errors such as AttributeError and ensures object integrity on creation.
We'll cover the following...
Access missing attribute
If we don’t explicitly set the x and y positions on our Point object, either using move() or by accessing them directly, we’ll have a broken Point object with no real position. What will happen when we try to access it?
Well, let’s just try it and see. Try it and see is an extremely useful tool for Python study. Open up the interactive interpreter and type away.
The following interactive session shows what happens if we try to access a missing attribute:
Well, at least it threw a useful exception. We probably seen them before (especially the ubiquitous SyntaxError, which means we typed something incorrectly!). At this point, simply be aware that it means something went wrong. ...