Instantiating With Attributes
In this lesson, you’re going to be instantiating new objects with attributes. Take a look at the code on-screen right now. This is the starting point. You have a class, a
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
00:27 Now, you’ve looked at instantiating this class briefly before, but in this lesson, you’re going to go into a bit more detail about it.
So say you try to instantiate the class that you just saw on-screen with
Point(). You’re not passing anything in to the call to the
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,
Because your constructor has an
x and a
y, you’re going to need to pass this in unless you’ve defined your constructor with optional parameters that have maybe a default value.
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
So you’ve got two points here. One is representing a potential origin point,
0, 0, and the other one a target point of
10, 15, for example.
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.
02:41 One thing to note about attributes in general with classes in Python is that you can change these things at runtime. It may not be a good idea to do that, but it’s something you can do.
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.
So here you’ve gone to the class, and you’ve changed the
.dimensions class attribute to
3. It changes on the class, and it changes on the instance as well.
So to sum up, if you have a constructor, you need to pass all the arguments apart from
self, which is populated automatically, or else you get an error.
03:39 If the constructor actually assigns instance attributes during that function execution, then you can access them from the instances, and you can also change them at runtime.
03:51 If your class has class attributes that aren’t in the constructor, you can access them from the class or the instance, and you can also change them at runtime too.
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.
So if you were to just instantiate
Point without anything, then you’d get an error because you need to include the
So say we take the
origin and we say that’s a
Point(), and the origin is usually
0, 0, so we’ll just put
0, 0 as the arguments of
So now if you look at
origin.x, you’ll get
origin.y is also
0. You can also change these, so if I wanted to change where the origin was, I could say
origin.x = -10, for instance.
So now if you look at
origin.x, you’ll see that it’s
-10. Let’s change it back to
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.
So now you might be able to do things like create a shape, which you might create a list, and you’ll say
0, 0, starts at
0, 0, and then create another point.
Maybe that goes up to, say,
5, 5. And then we’ll do another point, so this is like a triangle, say
2, 3. And I’ve spelled this wrong, so let’s just copy this …
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.
And then since you’re getting this point back, the last point from the
shape evaluation, you can chain on a
.x to get the
x position, which is
Become a Member to join the conversation.