Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Python is fundamentally an object-oriented language. In fact, in Python 3 everything is an object. So let's look at how we can create our own objects. We'll start by making a working copy of syntax.py. I'll call this syntax-objects.py. And we'll open our working copy. Now we'll go into a lot more detail on objects later on. The point here is just to get a handle on the syntax of creating objects.
So we'll go ahead and start by defining a class. Now a class is the definition that's used when creating an object in Python. So we'll use the class keyword, and we'll call this class Egg. And the first thing we'll define inside of the class is a constructor. And a constructor in Python is a method, or a function inside the class, and has a special name which is __init__, like that. And all methods within classes have self as the first argument.
This is a reference to the object itself. And it's traditionally called self. It's not required, but it's a really, really good idea to always call it self. And following self can be arguments that can be used in constructing the object. And in this case we are going to say how do you want your eggs? And so it'll be a kind of eggs, and we'll call it kind, and we'll give it a default value of fried. And then we will assign this value to an object variable.
And object variables are referenced to the object itself, so we'll say self.kind = kind, and that will create an object variable that will be carried around and encapsulated in the object. And so each instance of this class, each object, can have a different value for kind. Now let's define a method for finding out what kind. And we'll call this whatKind, and it has self as the only argument.
And we'll simply return self.kind. Now we have a class. It's a very simple class, and it doesn't do anything terribly useful, but it's good as an example of the syntax of how you create a class. And now we'll go ahead and create an object. Now an object is an instance of a class. You can think of the class as like the blueprint. This defines how the object is created, and the object itself is an instance. The object is an encapsulation of all of the methods and the variables that are inside of the class.
And so we'll say fried = Egg, like that. And that will create a object called fried, based on the class, Egg. And you notice we don't have anything in the parentheses, so it will use this default value when it creates or constructs the object. The constructor is called every time you create an object. So here we've created an object with the assignment operator. And so the first thing it'll do is it'll call this Constructor, and it'll assign this default value.
We can also create a scrambled egg, like this. And so when we print these out, later on we can say print(fried.whatKind()). And if we save this and run it, it says that it's a fried egg. Instead of the fried kind, I used the scrambled kind, and this is just this different object, but it's using exactly the same interface.
If I save it and run it, now we have a scrambled egg. So this is just a very simple and a very simplistic example of how an object is created. We have a definition, which is called a class, and this defines the blueprint of how these objects are made. And then we have a couple of objects that are created using that class. And so these are considered instances of that class, or objects. And these objects are completely functional, and they contain data, and they contain code in the form of these methods, and that is how you define and create objects in Python.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105185 Viewers
56 Video lessons · 116967 Viewers
71 Video lessons · 86167 Viewers
131 Video lessons · 41205 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.