14.1 - Object Oriented Definitions and Terminology
So this is kind of hiding abstraction, there's a lot of words that we use to basically say, don't worry about what goes on inside. And like I said, we have been using objects the whole time, strings, integers, dictionaries, lists, floating points, etc. But another way to say it is it's got code in it, and we call a function inside the object, and they're kind of equivalent, but sort of just one to some people seems prettier than the other.
- زمان مطالعه 8 دقیقه
- سطح متوسط
دانلود اپلیکیشن «زوم»
این درس را میتوانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید
متن انگلیسی درس
So welcome to the chapter on object oriented programming. I have been using terminology all along about object oriented programming. I say object, like BeautifulSoup returns the soup object. And then I call the find method within the soup object and I get back a list, blah, blah, blah. And I didn’t want to teach you object oriented programming too early in the class because until you need it, you sort of don’t need it. So I’ve been using these words without defining them. So guess what? Now is the time that we’re going to define these words. I’m not going to ask you to write objects. I’m not going to ask you to build your programs using object oriented techniques. You will eventually love OO, and object oriented. And you will be good at it. But in this class and in this lecture, I just want you to have a few words that you absolutely 100% understand. And I’m going to give you some sample code, but not so you can write that code, but to help inform your understanding of the concepts. because object oriented is something that I think you have to come back to a couple of times. You can’t just like, see it and go, got it. Like a lot of things in programming until you really need them, it’s hard to fully understand them. In other words, you’ve got to start and learn somewhere, and then come back again. So the word objects has been in, like I said, a lot of the words I’ve said, we’ve been reading documentation, they say the word object. What is object? Well, it says here the list data type has some more methods. Here are the ones that list of the list objects, and that means the objects of type list. And list data type has methods. Methods are these things that are functions built into objects. And I’ve said these words before and now I want us, by the end of this lecture, to know these quite well. So as we’re moving our next stuff is we’re going to move into databases. And database is a connection object and a cursor object and an object in a method and call a method. Again, I just want you to know these words. So let’s go back to the beginning and start with programs that we started with. And here was our first program that handled input, processing, and output. You can draw a boundary around this program, right? And then there’s kind of like the inside stuff and the outside world, okay? And to the outside world, this program does something. And frankly, if this is the outside world, we do not have to keep track of the detail of this. Like we’ve got this program, we installed it on our computer, we’re confused about floors and we’re in Europe and we say, yeah I’m on floor 0. What would be the equivalent US floor? Thank heaven I have this application, right? And so there is this notion of kind of like inside and outside, where we hide the detail. Like programmers have to worry about this detail, the programmer that wrote it, but a lot of people don’t. So this is kind of hiding abstraction, there’s a lot of words that we use to basically say, don’t worry about what goes on inside. And so in actuality, that program had several objects in it. The string object, there’s an integer object. And so what happens is these objects also sort of are like containers of functionality, containers of awesomeness. And these objects sort of work together. And the program itself kind of orchestrates these objects, one or more objects working together. And the best way to think about an object, it’s kind of like a program but smaller. It’s a little thing that we can draw a boundary around and say, there’s some code and data in there. There’s logic, there’s data, and we do stuff with it. And the idea is you take this big problem and break it into a series of problems, like small Lego blocks, and then build these things back up to build your program. And like I said, we have been using objects the whole time, strings, integers, dictionaries, lists, floating points, etc. And so if you think of a program, it’s going to have different kinds of objects that are in it and these objects are going to work together to take the input of the program and produce the output of the program. And like I said, we’ve been doing this all along. But one other thing about objects is that each one contains some code and some data. And you can send stuff back and forth between objects, you can make new objects, you can do stuff with objects. But one of the things is like okay, if you’re in the object, you just say oh, this is what this object does and I’m not going to worry too much around the world in which I’m embedded. And the same thing is true about the outside world. We’re going to use this object and we’re not going to look on the inside of it. So from outside looking in, you ignore the detail. From inside looking out, you ignore the outer detail. And so it’s a way to draw a nice bright line and say you worry about this and we’ll worry about that. It’s a separation of concerns. And so some terms that we’re going to be using are class, object, method, and attribute. And so a class is a shape of an object. It’s a template. In the little example, it’s like the cookie cutter and then the object is like the cookie. And so you can say, I would make a bunch of objects. So the string is a class. We have as many strings, we can have a string called x, and we have a string called y, and a string called z, and x could have something different than y and z, etc. But the class is string. And then methods are like uppercase. string.uppercase, that’s a method. That’s a capability. And attributes are data that’s part of a class as well. And objects, and so we’ll get to these. So class is defining the general characteristics of a thing like what fields it has, what properties it has, it is a blueprint for making things, right? Class Dog is all dogs but then you would make, each dog then would be an object. Then the concept of dog is like a class. But when you see a dog and you grab the dog, that’s an object. An instance is another word for object. And so this is where we have Dog and then we have many actual dogs, or cookie cutter and then many actual cookies, okay? And so the object or instance is the word we give to the real things. Not the shape of the things, but the real things. Method is a part of defining a class, it’s also part of the object. And that is things like string.upper, string.startswith, string.find, those are all methods in the string class. But they also are part of every string object. In some forms of object oriented sort of the simpler, prettier, pure, they would call it a message, meaning that here’s this object, and you’re going to send it a message to cause it to do something, you would kind of poke it with this message. But another way to say it is it’s got code in it, and we call a function inside the object, and they’re kind of equivalent, but sort of just one to some people seems prettier than the other. For me, I tend to think of it as like code. We’re calling a function that lives in the object. So like I said, we’ve been playing with these objects all along. We have a string object and when we print this type thing out, now we’re going to understand a little bit more. And that basically says that x is an object of type class, the class string, right? So that’s the kind of thing, this is an instance of the string class. This is an instance of the float class. This is an instance of the integer class. We make a list, say what is it? Well, this is an instance of the list class. What is this? Type z is a dictionary, and that’s a class. Now, so these are all the classes. Those are the templates. You can have many strings, many instances. An object, this is the object, this is an object, this is an object. We can also ask, based on these things, what are the capabilities? And these, that we have seen before, are the methods. In our x, we’ve got stuff like upper. That’s one of the methods. In y, which is our list, we can append, we can pop, we can remove, we can sort. So there’s all kinds of methods in lists and in dictionaries. We’ve got things like items, values, get. Remember get, how we use it in counting? well, get is a method that is part of all dictionary objects. So we’ve been playing with these things all along, it’s just that now we want to make this terminology nice and precise. So up next, we’re going to talk about how you would build your own class in Python. And it’s not so much that I want you to build a bunch of classes but really to help as you look at how we define them to kind of think as how a string class might be defined or how you use a string class.
مشارکت کنندگان در این صفحه
تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.
🖊 شما نیز میتوانید برای مشارکت در ترجمهی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.