Instantiating With Attributes
It has a class attribute of
dimensions = 2. This will be available from all the instances. And then you have your constructor that defines two instance attributes,
self.y, because every point needs an
.x and a
If you do this, you’ll get an error. The main error here that’s being raised is a
TypeError and the message is telling you what’s going wrong:
__init__(), the constructor, is missing two required positional arguments,
That’s not the case here, so you’re getting an error. If you want to properly instantiate with attributes, you need to pass in two arguments. For example, if you have a point which you want to represent as the
origin point, you’d have to pass in
Say you had another point,
target. You’d pass in
10, 15. Now these are just arbitrary numbers, but you do need to pass in something for the object to be instantiated properly because of the way you’ve written the
The instance attributes are available by just referencing with dot notation (
.) the attribute that you want. So if you want the X position of the origin, you do
origin.x, and if you want the Y position of the target, you do
target.y. Those give you the values that you’re looking for, but the class attributes are available on both the instances and the original class.
So here you can see you’re getting the
.dimensions attribute of the
Point class, which is
2, but you can also access this from an instance, and even though you haven’t instantiated any kind of instance attribute in the constructor method, you still have access to
.dimensions from all the instances.
So instead of the
15 as it was, you can change the target to equal
12, and then if you evaluate that, you’ll get
12 back. You’ve changed it. Likewise, if you say what are the dimensions of the target, well, you can change the
.dimensions attribute anywhere, and it will update both on the class and the instances.
04:02 Whether that’s a good idea or not really depends on the use case, but usually it isn’t a good idea. Usually if it’s a class instance, it’s something that you probably want to stay quite static throughout the lifetime of your program.
So let’s check out instantiation on the IDLE Shell. You have your class
Point defined up here with a class attribute and a couple instance attributes that are defined in the
.__init__() constructor function.
05:18 because that makes more sense as an origin point. You’ll note that you can also access the class attributes from this instantiated object. You can also access this class attribute from the class itself, and you can also change it from these two as well. Although again, class attributes are generally things that you may not want to change at runtime, but really that’s up to you. It’s just not a very common pattern.
fix that. So now you have a
shape list with three points in them, and if you wanted to access one of the items in that list, you could say
 and then
shape resolves to a point, the last point in this list.
Become a Member to join the conversation.