Understanding Object Oriented Programming with ChatGPT

Michael uses ChatGPT to teach Ilana object-oriented programming basics, working through classes, objects, attributes, and methods with concrete examples like a library system and bank account. Ilana asks clarification questions and connects concepts to her own coding experience, showing how a language model can serve as a personal tutor for iterative learning.
Chapters
So I wanted to check in on Python first. How is Python going this week?
It's going good. I had to miss 2 of the days, I think, this week. I was busy and I didn't have my computer with me. But I think it's going good. It's definitely getting harder, so I'm enjoying that.
Are you still in network programming or have you gone on to the next section?
I think I'm on the next section.
Do you remember what that one's called?
It's called web services.
Are there any hard concepts in that one?
I think there were hard concepts in definitely the last few that I've done. I, I, it's just, it's just kind of confusing. I guess you're gonna have to take the time to learn it.
Got it. Um, did you— I suggested using, um, you know, ChatGPT to learn new concepts. Did you try that at all?
I haven't yet, but I want to set it up when I get home. Ask more about it.
Have you, have you looked at ChatGPT at all?
Um, I think my dad has it. And so I spend maybe a few minutes with it, with him.
Got it. Well, I thought we would try it just a little bit and see. I have never actually used it to learn myself, so I thought we could try it. So I'm going to try sharing my screen. We'll see if this works. You can see my screen?
Yes, I can.
Okay, so this is— I've logged into ChatGPT just using my Gmail. Have you ever seen this before?
No, I think my dad already had it open.
Okay, so I'm not even sure how it works. I tried it a while ago, but I know the next unit is on object-oriented programming. And do you have any idea what that is? See what it says.
No, I don't.
Okay, I was going to see if we could use it to figure out what object-oriented programming is. But I'm not, it's not allowing me to, to do something. Let's see. Test. Ah, there we go. So I, I was wrong. It said new chat over on the left. It's down here. So can you, I'm gonna ask it to explain object-oriented programming. Can you explain object or whoops, objections, too much autocorrect there, oriented programming? Certainly. Okay, here we go. So isn't this cool?
Yes, it is.
It's very cool. Oh wow, lots of stuff. It's going to be long and complicated. We'll see if we can make sense of it. So we'll— I'm going to let it get done, and then we'll go to the top. Let's see how long this is going to be.
Okay. Um, I think in my humanities class somehow we got into the topic of ChatGPT because we were talking about something, and some of my classmates have it, and so they said in the chat, my ChatGPT said blah blah blah blah blah, and it was very, very long.
Yeah, yeah. So I'll read the first paragraph and we'll see if we can make sense of it. Certainly. Object-oriented programming, OOP, is a programming paradigm based on the concept of, quote, objects, which can contain data and code to manipulate that data. OOP is widely used in software development because it helps to organize code into reusable modular units. So what makes sense about that and what does not make sense about that?
Well, to start off, helps to organize code into reusable modular units. I don't really know what that means, but the first thing that comes to my mind is just making it more organized and not so messy. I don't know if that's true, but I don't make much sense of it.
Let's, let's, let's see. Let's see what happens. So I'm thinking, let's ask this just like it was a person. Okay. So what do you mean by reusable? I'll do the whole thing. Organize code, organize code into reusable modular, modular units. Can you give me an example? Give me an example. So let's talk to it just like a person, okay? And see what it— Yes. And it's not perfect. There's another one called GPT-4, which is supposed to even be more like a person. But— Oh. Okay, so look, it's just answering her questions just like that. Isn't that amazing?
Very.
So this is why people, including myself, are excited about GPT as a learning tool, because, you know, you can— it's like a personal tutor. So sure, you'd ask— you, Ilana, had asked, what do we mean by organizing code into reusable modular units? They say, sure. Notice how helpful they are. I guess they say sure with an exclamation part. One of the things in designing this is I think they want to make it sound like a human. Organizing code into reusable modular units means breaking down the code into smaller self-contained components that can be used in different parts of a program or in different programs altogether. These components are designed to perform a specific task and can be easily combined with other components to build more complex functionality. Does this make any sense?
I think it makes some sense. Modular units, breaking down code into smaller, just maybe bits of code that are more organized.
For me as a learner, I always want examples because sometimes, you know, they— I, I would say it did seem to clarify it a little bit, but I see examples coming down here. And so as a learner, I'm eager to say, come on, give me the examples so I can figure it out. You know what I mean?
Yes.
So for example, let's say you were building a program. And by the way, that's why I asked it to give me an example. Because I know myself as a learner, I know that I need to learn examples, concrete examples to learn abstract concepts. That's why I thought, let's give it, ask for an example.
Yeah.
For example, let's say you were building a program to manage a library of books. Instead of writing all the code for the program in a single file, you could break it down into smaller reusable components such as a book class that defines the property of a book such as title, author, and publication date. I'm gonna check in the bullet points. Does that bullet point make sense?
I think it does.
So, I mean, you have, I, you have a library of books, right? You've got a bunch of books in your room that are yours.
I do.
Do you organize them? Is it organized into a library?
It's not organized like A through Z or publication date, but it doesn't look that messy.
Okay. Well, I have a lot of books, and my books are a total mess. I have to— I need to organize my books. So this could be useful for me. So a book class that defines the properties of book, title, author, and publication date, those are pretty basic. A library class that manages a collection of book objects and provides methods for adding, removing, and searching for books. What do you— does that one make sense?
It does.
So what I'm thinking is, suppose I, I gave the program, or, or there was a way to input the data of title, author, and publication date for the book class. What I'm, what I'm getting out of the library class is Every time you add a book or remove a book, or even when I search a book, that's sort of a different thing than a list of all the books. And then a user class that represents a library user and provides methods for checking out and returning books. Does that make sense?
Yes.
Yeah, I'm, I'm thinking, oh yeah, I need to know if Michael Strong is taking my books out, or if I'm loaning a book to Alana Garcia, or what it is. So user, that makes sense. And a GUI, a graphical user interface class that provides a user-friendly interface for interacting with program. Do you know what that is?
I don't know what a GUI is, but it says providing a user-friendly interface. So maybe just the code for the actual website or screen, not website, but most of Most user interfaces, the user interface is how you interact with it.
And like when you're doing the Python code itself, it's all code. You don't get pretty, pretty images and stuff. But are you an Apple user or Microsoft or?
I'm a Windows. My computer is a Windows.
But you have a desktop and on the desktop, you know, you have files and you have a garbage can and stuff like that, right?
Not specifically, but yes, something like that.
But you've got a visual interface. And by graphical, they mean basically visual. So the point is what— and this started with Apple, but now Microsoft has copied it. What you've got is a graphical user interface instead of just code all the time. When you're right now, when you're programming in code, everything is typewritten, it comes back typewritten. You don't have visual images that you can click and drop and things like that. Make sense?
Yes.
So, so now we've got 4 reusable. So these are reusable components, a book class, library class, user class, and a GUI. Then it says each of these components can be written and, and tested independently, making it easier to debug and maintain the code. They can also be reused in other programs that require similar functionality. Does that make sense?
Yes, it does.
So I'm seeing another paragraph with another example, and I love examples when I'm learning stuff. For example, if you were building a program to manage a music library, you could reuse the Book class and modify it to create a Song class. You could reuse the Library class and modify it to handle music files instead of books. This approach saves time and effort in development and leads to more robust, scalable programs. Make sense?
It does. Yes.
So what I'm getting from this is that instead of just code right now, you— have you ever reused any, any parts of the code you've written so far? Or every time you— you have you reused parts of the code?
I have.
Do you remember an example of how you've reused parts of the code?
When I was creating just a small project and I had something similar and I forgot what the exact syntax was, I just went to a previous project and pulled out the code.
Got it. That makes a lot of sense. And was— I mean, in some ways, I don't know if that piece of code, since I don't know what it was, but I wonder if that code could be considered an object. That is, you know, what I'm getting is objects are kind of pieces of reusable code. Does that make sense?
Yes, it does.
But I'm curious, I want to know if I'm right. So let's ask ChatGPT, am I right? In OOP, is any reusable piece of code considered an object? So here we're checking to see if we are, or I'm checking to see if I understand this correctly, or if not. Aha, not every usable piece of code is an object. So here I thought I might have understood it. I thought maybe your piece of code may have been an object, and it's telling me nope. Let's see how complicated this is. But you know, sometimes you think you understand something and you might ask the person, did I get it right? And so that's exactly what I'm doing here. I thought I understood it, but it's saying, nope, Michael, you did not get it right. In OOP, not every usable piece of code is considered an object. An object is a specific instance of a class that contains both data in the form of attributes and behavior in the form of methods. So I've gotta stop right there. Maybe since you've been coding, I haven't coded in decades, since you've been coding more recently, does this, do you know what an attribute is?
I do, yes.
So can you explain it to me? Be my ChatGPT.
Well, it might not, or this, I'm thinking of an attribute as in the Python that I write for making games. In that sense, an attribute is something you stick with the actor that you're moving. For example, you could stick a variable onto it and name it speed, xSpeed or ySpeed. And I think that would be considered as an attribute. Got it. And then whatever variable.
Does that make sense here? An object is a specific instance of a class. That contains both data in the form of attributes and behavior in the form of methods?
Yes.
Do you know what they mean by behavior in the form of methods?
Um, I mean data in the form of attributes and then behavior as in what you're doing, maybe in the methods that you create.
Okay. It sounds plausible, but I honestly don't feel like I have a super solid understanding. Maybe I need to kind of catch up on programming. But let's get a little bit more and see if one of us can make sense of this. Reusable pieces of code that are not objects can include functions or procedures that perform a specific task, but do not have a state associated with them. So do you know what they mean by functions or procedures?
I think so. I know what functions are and procedures are maybe just tasks.
Yeah, I have a pretty good sense of functions and procedures, but when they add do not have a state associated with them, I'm not too clear on that. Do you know what they mean by that?
No, I don't.
So let's ask again. What do you mean? The nice thing about this is, you know, because it's a machine, it will never, ever, ever get tired of answering questions. You know, with human beings, it's like, I don't get it. Can you, you know, and people should be patient. But what do you mean by do not have state? Actually, there is an example we could have used, but let's see what it says here. State refers to the value of an object's attributes in a given point in time. An object's state can change as its attributes are modified by the object's methods or external factors. So it's giving us another example. Okay, so when we say that a piece of code does not have state associated with it, we mean that it does not maintain any internal state or data that can be modified by its methods or by external factors. The code simply performs a specific operation or calculation based on the input parameters it receives and returns results. So I'm pretty fuzzy there, so I want an example. Are you Do you kind of get a fuzzy understanding of that?
Yes, I don't understand it fully yet.
Yeah, that's what I mean. I can sort of kind of guess, imagine that I kind of sort of think what it means, but not super clear. But an example might land the plane for me. For example, a function that calculates the area of a circle based on its radius does not have any state associated with it. Okay. If it takes the radius as an input parameter, performs a calculation based on that input, and returns the result. The function does not maintain any internal data or state that can be modified by its methods or by its external factors. How about that one? How's that paragraph doing for you?
I think that makes a little sense because the function doesn't have any internal data. It can't— or state it can't be modified. I can sort of make that in— I can sort of play that in my mind.
Yeah, I am eager for this last paragraph. Again, when I'm trying to understand things, sometimes I understand it sentence by sentence super carefully. Sometimes I got to get the whole thing and read the whole thing a couple of times to make sense. This is where I need the in contrast part. In contrast, an object has state associated with it because it contains attributes or properties that can be modified by its methods or by external factors. For example, an object that represents a bank account might have attributes such as the account balance, the account holder's name, and the account number. The state of the object changes as its attributes are modified by the object's methods, such as deposit or withdraw. So this example of attributes is helpful to me. Is it helpful to you?
I think it is.
So what, what I'm getting is here it used— I'm going to try it in my mind. I often like to test to see if I understand it. So attributes would be account balance, account holder's name, account number. In the case of the book library, an attribute would be the book, the book title, the publication date, In the example of the user, the user might be, you know, name a user, how often they check out books. That is the attributes. Yeah, somehow I'm getting— trying— could you— can you explain to me how you think the circle example is different from the bank account example?
Well, the bank account, it— you're changing the attributes and data inside of that function or whatever that's in. And the circle, you're not really changing anything if it's in contrast, and you don't— besides the, um, radius that you input, it doesn't change that. And so it's not Or you can't change it just because it's an input. And that differs from the bank account example.
Yeah, something like that sounds right. What I'm kind of getting is somehow, for some reason, I don't understand the why, for some reason, they only want to call things objects and object-oriented programming if it's got— I'm going to use a non-technical term— a bunch of stuff attached to it. Whereas a simple function, like a function that calculates the area of a circle, does not have, I guess I should use their language, attributes attached to it. Therefore, it's not an object. Is that consistent with your understanding?
Yes, it is.
So let's check to see if we're right. So a function alone Without any data or attributes— I'm still not perfectly clear on attributes, but I kind of get to it— get it is not an object when it is object in object-oriented programming, correct? Let's see if we got the answer right this time. Hey, voilà! It likes us now. I'm joking, obviously, it's just a computer, but you know, it's nice to get the right answer. Okay, and because I want to let you go, I don't know that we— is it okay if we don't finish this for now?
Yes, but we can finish it if you'd like.
Okay. Well, let's go ahead and read this then. You're on vacation. I just wanted to be respectful of your time while you're on vacation.
Thank you.
That's correct. In object-oriented programming, a function alone without any data or attributes is not considered an object. An object is a specific instance of a class that contains both data and behavior in the form of attributes and methods. I kind of have the attributes thing from the bank account and the record example. Personally, I don't have the methods thing. Do you have the methods thing?
No, not really.
So we're both in a similar— sounds like, you know, Maya, I used to program and I've forgotten, and you, you're just learning to program. Sounds like we're kind of at the same place. Yeah. A function, on the other hand, is a self-contained block of code that performs a specific task or operation based on its input parameters, but does not have any state associated with it. Personally, it seems a little bit weird to me they call it a state associated with it, but I'll go with it. Have you heard that language before in coding, a state associated with it?
I've heard the word state, but not in this context.
Yeah. Often, you know, I'm very aware of when someone is using a word in a new way. If people are using words in a familiar way, I totally understand them. But if they use a word in a new way, I'm like, what? And so I'm just flagging that I don't quite know what they mean by state in this context. I can guess, and guessing is legit, but it may not get us there. Functions can be used in object-oriented programming to perform operations on objects or to implement methods of a class, but they're not themselves objects. Okay, so I know what a function is. Now, now I know it's not an object. Object needs stuff with it. In OOP, objects encapsulate state and behavior while functions encapsulate behavior only. Okay. So the way I'm getting that is, you know, if you've got the, all the data from the bank account or all the books in the library, the object would have data about those, but the function wouldn't. Does that seem accurate?
I think it does. Okay.
However, functions can be combined with objects to create more complex behavior and functionality. And they can also be used in a procedural programming paradigm where they are the building, primary building blocks of the code. Okay, so I'm feeling like I have at least a 1.0 understanding of at least the difference between a function and an object. How about you?
Sort of. I know what a function is, but not so clearly on object.
Maybe let's go back up and see if an earlier example, or is there a question you would like to ask it?
No, I don't have anything in mind.
Okay. Well, maybe, maybe, but we can, we can stop now. But do you see how you can use this as a learning tool?
Yes. That's useful.
It's pretty amazing, isn't it?
It is.
So, you know, I don't know when you'll get a chance to do it, but I would encourage you to, you know, play with it and maybe you and— I've forgotten your fellow learner, your fellow Python student. Colette. Colette.
Yes, Colette.
Maybe you and Colette, if you know, you could try it just because I think it's cool. I think you see how you have to ask it questions in order to get something out of it, just like you have to ask Sowalaki or another human being questions. And actually, you know what, I'm going to go back because there's one other experiment I want to do. I want to ask if it can give us a simpler answer because it was a little bit difficult. So let's see. I'm gonna ask, can you explain this more, more simply? Let's see what it does if I do there. Sure. Again, it's super cooperative.
Yeah.
It still is using similar language. Um, and so I think part of it is for me as learner, I skimmed it. Are you pretty good at skimming things quickly?
Skimming, or what do you mean by skimming?
Skimming the words quickly.
Yes. Okay.
Have you kind of looked at what it said already, or do you wanna read it?
Um, I'm gonna read it. I couldn't skim it while it was going.
Okay.
Technically.
Okay. You want to read it out loud? I've been doing all the reading out loud.
Yes, I will. In object-oriented programming, OOP, an object is a specific instance of a class that contains both data and behavior in the form of attributes and methods. An object can be thought of as a self-contained entity that represents a real-world concept or entity. On the other hand, a function is a self-contained block of code that performs a specific task or operation based on its input parameters, but does not have any state associated with it. A function can be thought of as a procedure or routine that performs a specific operation. While both objects and functions are used in programming, they serve different purposes. Objects are used to represent real-world concepts, objects or entities and to encapsulate data and behavior, while functions are used to encapsulate behavior only. In summary, an object is a self-contained entity that, that contains both data and behavior, while a function is a self-contained block of code that performs a specific operation but does not have any data associated with it.
Does that help you?
It did.
What would you say you got out of that that helps to clarify?
I think that I got a better understanding of an object, how it encapsulates both the data, I believe, the data and the behavior. I'm still not so clear on what behavior is, so I'll have to look that up sometime. I think that— I think I got more out of that though.
Let's just check. What do you mean by behavior? I like how helpful it is because it's just a computer. It's not like— but it is sort of magic how it's just very helpful.
Yes.
Okay. In the context of object-oriented OOP programming, behavior refers to the actions or operations that an object can perform. It was interesting. I had to stop and think there for a minute.
Yeah.
An object's behavior is defined by the methods it has. Methods are functions. That's helpful. Methods are functions that are associated with an object and can operate on the object's internal data called attributes or properties. For example, let's say we have an object that represents a car. This car object might have attributes such as make, model, car mileage, methods such as start, stop, accelerate, brake. That's helpful. The start method, for example, might perform actions such as turning on the engine, engaging the transmission, and setting the car in motion. In this example, a car's objects— the car object's behavior is defined by the method it has, such as start, stop, accelerate, and brake. These methods define the actions that the car object can perform and provide the functionality that the object offers. In summary, behavior in OOP refers to the actions or operations that an object can perform, which are defined by the methods as. How was that helpful?
It was, it was definitely helpful.
Okay. Well, again, we could go on with this for a long, long, long, long, long, long time, but I just wanted to show you what a super cool tool it is for learning. And you know, one way to think about it is this is another way to give you superpowers. So anytime you are getting stuck on anything, you know, you can Google it, but I would say this is more helpful than Googling.
I think so, because Google gives you so many different answers, and most of the time they're not on what—
they're not what you wanted Exactly, exactly. All right, well, that was your introduction to ChatGPT for Learning. So have a wonderful rest of your trip, and we'll see you soon.
Okay, thank you, Michael.










