Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Instantiating With Attributes

00:00 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 Point class.

00:11 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.x and self.y, because every point needs an .x and a .y.

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.

00:36 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 Point constructor.

00:46 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, x and y.

01:03 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.

01:16 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 0 and 0.

01:32 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 .__init__() constructor.

01:48 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.

01:57 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.

02:20 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.

02:53 So instead of the target.x being 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.

03:18 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.

03:30 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.

04:17 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.

04:32 So if you were to just instantiate Point without anything, then you’d get an error because you need to include the x and y.

04:40 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 x and y.

04:53 So now if you look at origin.x, you’ll get 0, and 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.

05:09 So now if you look at origin.x, you’ll see that it’s -10. Let’s change it back to 0

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.

05:48 So now you might be able to do things like create a shape, which you might create a list, and you’ll say Point() maybe 0, 0, starts at 0, 0, and then create another point.

06:02 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 …

06:25 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 [2] and then .x, because shape[] resolves to a point, the last point in this list.

06:45 And then since you’re getting this point back, the last point from the shape[2] evaluation, you can chain on a .x to get the x position, which is 2.

06:59 And that’s instantiating objects.

Become a Member to join the conversation.