Placeholder Image

字幕表 動画を再生する

  • Hello, world.

  • This is CS 50 on Twitch.

  • My name is Cole Tanaka.

  • I'm joined today by Brian You, who has been upstream before.

  • So previously we talked about react.

  • Normally we build something from scratch.

  • But sometimes you do conceptual discussion.

  • So what we're talking about today, Today the topic is going to be object oriented programming, which is a programming paradigm that you'll find in a number of different languages today.

  • In particular, we're gonna be looking a python, though there's object oriented programming in languages like Java, for instance, and many other languages as well are beginning to use this paradigm, and it's quite popular in the programming world.

  • So C s o CS 50.

  • What?

  • What way?

  • Sort of teaching CS 50.

  • How does object oriented programming sort of compare and contrast to that?

  • Yeah.

  • So the style or programming paradigm CIA 50 talks about is this idea of what we might call imperative programming on what object oriented programming is.

  • It's a slightly different take where the focus is about trying to model Rhea World things as what we might call objects.

  • And those objects are gonna have two parts to them.

  • Objects might have some data associated with them, so you might have an object that represents, like a shopping item, for instance, in a store.

  • And that item has a name.

  • It has a price that might have an image associated with it.

  • For instance, on those objects can also have some code associated with them code that you can use to operate on or manipulate those objects.

  • That you might have a way to purchase.

  • A shopping item, for instance, might be some code some action that's associated with that particular object that you can use to make something happen.

  • Kind of taking it above the sort of bare metal of the computer a little bit more than something like See where you're literally coding instructions that kind of roughly mapped out the CPU talks in innocence.

  • Yeah, exactly.

  • It's often a feature of higher level programming languages that allow you to reason a little bit more abstractly you to organize code in particular ways.

  • We're gonna be able to write code that we probably could write in an imperative way.

  • You thinking CIA 50 style programming, but this is just going to offer a slightly different way of achieving the same thing, and it's ultimately going to allow for amore clean way of running certain types of code.

  • Cool.

  • And then we'll be using python today, python being one of the more popular languages currently used.

  • But this is a paradigm that we see it all over the place and particularly job.

  • Some folks in our community might be familiar with Java, and John was a very, very object focused language where you literally everything has to be an object in Java.

  • Let me go ahead.

  • If you're ready, I can switch over to computer if you want.

  • Yeah, sure.

  • We'll go ahead.

  • Guess on our trip to object oriented program.

  • All right, so we go.

  • Sorry.

  • Screen.

  • Got a little bit glitch.

  • They're good.

  • Uh, you can keep going on with that.

  • Stretch the screen.

  • All right.

  • Sounds good.

  • There we go.

  • Perfect.

  • All right.

  • So I have here a terminal window, and what I thought we do is dressed as a bit of an introduction to python.

  • Now, just go ahead and type python, which is going to open up a python interpreter.

  • I'm using python version 3.7, But any version three or later will probably be sufficient for the sorts of syntax that we're going to see today.

  • Andi In this python interpreter, I can begin to type valley the names of expressions or values, and I can see what they're equal to.

  • So the number I typed in the number 50 for instance, and what I get back from the Interpreter is OK.

  • This is the value 50 on.

  • Of course.

  • That's obvious, cause I typed in the number 50 exactly, but you can type more complex expressions, right?

  • I could type an expression like 20 plus eight, and when I get back is not literally 20 plus eight.

  • But I get that expression which get the value in it to a value.

  • And that value in this case is 20 for instance.

  • And so this 28 here it's a number in particular, it's an integer, and if I type in the number 28 okay, its value is 28.

  • But there's a special function in python called type, which I can use if I say type 28 for instance, I can see all right, what is type And here we see the first instance of an object oriented programming word this word class class is going to tell me, sort of What type of information is this value?

  • The number 28 it's telling me, belongs to a class called int.

  • And so the way to think about this is 28 is an object.

  • It is a thing that exists is a value in python, and it belongs to a class.

  • What type of thing is 28 28 in this case is an end, and it happens to be one of the built in classes in python that I can use.

  • And in fact, any time you use an integer and python you're using, an object that is of this into class thrives on being more of a dynamic language.

  • It doesn't really have, technically speaking the same kinds of primitives that, like sea or even job, which Adam and Team didn't mention the chat.

  • Saying that he uses job doesn't primitive so that everything is an object I should clarify.

  • What I meant was everything is wrapped in an object.

  • For example, you can't write a main function without it being in a class.

  • I'm not an object, a class I should say.

  • Everything is wrapped in the class.

  • But in Python there are There really isn't technically speaking, the idea of a primitive at least in terms of, you know, this is 32 32 bits and that's it.

  • You know we have.

  • It's essentially wrapped in some extra packaging.

  • Yeah, so all of the even this basic integer 28 has some data and code associated with it that a normal just a pure integer wooden house.

  • So I could call 28.

  • Like Dodds.

  • Let's see what the options are.

  • Oh, sure, I could do 28 dot numerator, for example, and say, uh, that didn't work.

  • I said X equal to 28.

  • I can say ex stopped numerator and okay, I get The numerator of 28 is just 28 the denominator of 28 is one, for instance.

  • And that's not something you could do if we just had the literal bits representing the number 28.

  • It only works because this variable X is what we might call an instance of the class, and when I say instance, I just mean that if it is a class than any time I have an object that belongs to that class that's going to be an instance of the class.

  • So X, in this case is an instance of this into class.

  • So, like class being the blueprint and then actual 28 is the physical thing.

  • Start if we want to take it, You know, into a physical analogy, the physical thing we use the blueprint to create or construct exactly.

  • Yeah, Andi And doesn't want the only class is built in the python.

  • If I have the string Hello, for instance, that equal to just call It s for strings, Right.

  • So I have this variable called s.

  • And if I take the type of s I see that s belongs to a class called Stir.

  • It is an instance of this class stir where stars restored short for string, for instance.

  • And there are all sorts of functions that we have that are associated with objects that air an instance of the stir class.

  • So, for instance, capitalizes one that just takes a string and capitalized the lower case H turned into a capital H, for example.

  • And so that's an example of yet another class that we have that's built into the python programming.

  • All this nice functionality for free.

  • And this is a nice thing, toe.

  • What you mentioned earlier is that these classes allow us to sort of tie this functionality to different data types to allow us to go after right functions.

  • For every single thing separate from the data, it's kind of all packaged together.

  • Yeah, exactly.

  • So we have these built in classes in stir and their number of others.

  • But what I thought we do today is show you how you can using the syntax of object or into programming and Java or python rather create your own classes that have their own data that behave in their own particular ways because there are a lot of built in classes and python.

  • But there are certainly types of things that we might want to model that we might want to represent using data and code for which there don't exist classes in Python already.

  • Sure, what we can do is we can write some classes of our own that we can then use in order to create some functionality of our own.

  • So I thought we'd do if I go ahead and open up a text editor here, I noticed.

  • By the way, you were Adam user before and recently you've been using V s code.

  • Have you made that you made this sweet permanently?

  • Yeah.

  • I switched around text editors from time to time.

  • I used Adam for a time.

  • You sublime text for a while.

  • I've been using the S code lately as well.

  • Sometimes I'll just use them.

  • Bill.

  • Yeah.

  • Bab igniters that I use them often.

  • So you often seem using that as well, in particular in, like the Web programming class.

  • I think I used them for some portion of that as well, so it varies.

  • But at the moment, GS code seems to work pretty well as a quick aside, what have you found?

  • Any features of GS code that you like relative to Adam or vice versa.

  • Things that it's lacking that Adam has one really nice feature of Adam that they introduced actually recently within the past year.

  • So is the ability to do live Google Doc style collaboration on a text file.

  • But if I'm on Adam and I want to collaborate on a piece of code with my partner, I can sign with my get have user name and my partner consign in with his or her get hub user name, and we can just be working on the same file, typing live, seeing each other's changes at the same time.

  • That I think, is a very compelling feature of Adam.

  • It's very useful for para programming for working collaboratively, but I haven't really seen anyone quite be able to replicate in anything like that.

  • I'm I'm kind of disappointed.

  • GS code isn't It was unfortunate.

  • It would be nice if it did.

  • Matter of time, I think.

  • Yeah, maybe soon.

  • Maybe we'll see.

  • Apparently, visual studio live share does the same thing for being interesting.

  • I'm not familiar with that, but it's possible that they're extensions that you can add to these text editors that give them those features.

  • All of the big tech center, just nowadays, V s code sublime text Adam and many others have so many third party packages that you can install that had a variety of different features to the application.

  • And so finding the right packages could definitely be an important part of picking the right text editor.

  • Cool.

  • Okay.

  • Uh, what I'll do here is what I thought we do is create a class that's going to represent, like a book, for instance, a book that has a title and author, for instance, and maybe other data associated with it as well.

  • So I'm gonna go ahead and save this inside of a file that I'll call booked up pie.

  • Um, and what I'm going to do is I'm going to create a new class.

  • Remember, that class is the type of object that we're going to be creating.

  • I'm not right now gonna be creating a specific book.

  • I'm going to be defining the syntax for a class that is going to allow me to create as many books as I want based off of this class definition based off this blueprint for what it is that a book is.

  • So it's a class book, and this is my way in Python of defining some brand new class.

  • And the first thing that I need to do when I create a class is define some sort of function for what should happen when I try to what's called in Stan.

  • She ate this class.

  • When I try to create a book, what should happen and in python this function has a little bit of a cryptic name.

  • It's called Underscore.

  • Underscore.

  • In it underscore underscore, innit?

  • Standing for initial I've and Python often makes it special functions or special methods have to underscores on either side of it since just a python convention.

  • And so in it is going to be a function that's going to create a new book.

  • And whenever we have a particular functions that are going to run on, objects on objects that belong to a class were generally going to call these methods instead of functions.

  • It's just part of the syntax of the language of object oriented programming, the generally functions that operate on objects we call methods.

  • But effectively, you can think of them as like functions and this innit function.

  • All of the methods that operate on objects in Python will have this argument called Self, which is a little bit strange at first.

  • But basically self just refers is a way of naming the object that we're working with that we might be creating 5 10 20 different books and we want to be able to reference the data inside of each individual book and self is going to be a variable that is always going to refer Thio, the book that we're currently dealing with.

  • So when I create a new book, what are the types of information that I want to associate with the books?

  • This is the question when I create a book, what goes into the book?

  • Well, probably at minimum.

  • We need a title, for instance, and so I'm adding this title argument to the innit function.

  • Remember this innit?

  • Function is the code that is going to run when I try to create a book and hearing thing.

  • When I create a book, the book itself is going to be called self, but I also want to provide some additional information.

  • When I create the book, I want to tell the book what its title is going to be.

  • Okay, makes sense.

  • This is going to the parentheses when we create a new book in our code.

  • Exactly.

  • And we'll show that in just a moment.

  • And so now, inside the code of this innit method.

  • When I create a book, I want to store inside of the book inside of this self object, I want to store the title, and so what I can do is in any object you can assign values two properties of that object using dot notation in python, where I can say self dot and then the name of some property in this self dot property name where property name could be anything and give it some value.

  • Where that value could also be any python value, for example.

  • And in this case, the name of the property is just going to be titled.

  • I wantto change the name of the property in the book called Title to be equal to whatever this title argument is.

  • And so a common paradigm when you're creating objects is to say, Well, let's pass in the title when you first create the book And then what do I do with that title?

  • Well, I said it to be equal to a self duck title.

  • In other words, I take the self object, the book that I've just created it modify its title attribute and said it equal to the argument that I've passed it.

  • So a lot of syntax afar, some of which may be a little bit unfamiliar, but long story short, all we've done here is to find a blueprint for how to create a book.

  • I create a book by providing a title, and that's going to store the title inside of the book the dot being from with the most the newest thing for a lot of people.

  • And a lot of people might have used a module in Python are imported, you know, part of the standard library or something, and used it there but not actually seen it in the context of creating a class themselves or yeah, yeah, yeah, in the same way that we had, like, the number before, where I was able to say that number dot numerator or got denominator, That's the same idea of accessing a property of the object where the object stores a whole bunch of data and a whole bunch of code with it.

  • And if I want to access the data associated with an object, I just reference it by the name of that property.

  • Self dot title, in this case makes sense.

  • Well, you see there, why not use the properties like title inside the class assigned to a variable assigned to a very they're probably referring to what would be ecstatic variable in this case, okay, We might touch on the static variables a little bit later, or python will generally call them like class variables.

  • I guess that would be a good point to bring up on.

  • Maybe they're saying, Why not just create a new variable in here?

  • Why do I have to call itself dot title?

  • When way?

  • Sure, you could imagine that we could just call it variable T centered equal the title, for instance.

  • But remember that if you define a variable inside of a function, when that function is done running, we lose access to the variable so immediately after this innit function has done running this tea variable is no longer going to exist.

  • We can see that if I do something like, I'll define a function called F, and whatever is going to do is set X equal to 10.

  • For example, if I run the function f and then I say, OK, what is X like?

  • I print X to see what is the value of X.

  • I get a name in right X is not defined, even though I called F, which set the value of X up here after the function was done, executing I lost access to that Variable X because its scope, so to speak, is local to that particular function.

  • So after the function is done running, I no longer have access to this X.

  • The same thing's going to be true of this variable title.

  • This is just a variable inside of this function.

  • And so if I don't do anything as soon as this function is done running, I'm gonna lose access to that variable.

  • We might want to say, you know whatever book dot title later, right?

  • If I want to be able to access the title later after I've created the book, I need to store it inside of the self object somewhere.

  • That information is going to stick around even after the function is done running because the object still exists.

  • And so I'm storing it inside of self doubt title so that I can keep on Bihari weapons asking shouldn't first argument oven it Just be self.

  • The first argument.

  • Yes, the first argument of in it should be self, and it could be the only argument, but you're also allowed to have other arguments to in it as well.

  • That are other information that you want to provide When you are creating a book, for instance, And then Jack O.

  • T s is asking s o The word self has the same function as the word of this.

  • For instance, in Java script, just asking, not familiar with.

  • Yeah, exactly.

  • You compare itself in this case is very similar to this in Java script or other languages.

  • Just some word that represents the current object.

  • Let's go ahead and try and use it.

  • Let's try and create a book, so go out and tie Python.

  • And right now, the keyword book right book is not defined.

  • This is not something that's built into the Python language.

  • Specifically, it's defined inside of this file book, DuPuy.

  • And so I want to import from this pipe on file from this module, the book class, and so the syntax and python to do that is to say, from book That's the name of the module booked up high that I want to import the class from.

  • Let me import this book Class Capital B.

  • And right now I've imported the book classify type book.

  • All right, this is a class, and it's called Book Prince.

  • It's essentially extended the language, and in a way, we now have new.

  • Yeah, we have access to a new class, the new now that we didn't have access to before because we've just created this.

  • So if I want to create a new book, I call it be Well, say B equals book.

  • And then the argument to book is going to be the title.

  • The name of the book that I want to create on your favorite book.

  • Um, it's been a while.

  • Harry Potter against Harry Potter.

  • Harry Potter.

  • Uh, well, dio on the source of stone.

  • Which one was your favorite part of a favorite Harry Potter book?

  • That tough?

  • It is a tough one.

  • I was quite a fan of the fourth book, The Goblet of Fire.

  • I thought was very but they're all They're all quite excellent.

  • So we recommend them given.

  • All right, So I'm creating a new book and in the parentheses.

  • In providing the title of the argument, Someone likes Half Blood Prince.

  • Also a very good book.

  • Yeah, providing is an argument to book the title of the book that I want to create.

  • And I'm saving that book inside of a variable called B oppressor, turn and right I've created this variable called B B is a book object and in particular, if I say What is the type of B?

  • What I get is all right.

  • B is a book, and in particular I can no access the properties of this book.

  • If I want to get the title of the book, I can say be dot title, for instance.

  • All right, get Harry Potter and the Sorcerer's Stone and what might clarify?

  • I don't know if you made a buddy mentioned this.

  • Apologize if you did, but this self is implicit in on Line three.

  • You're We're not ever passing in self to the constructor for the class.

  • And so somebody maybe I could I could envision maybe somebody saying what we're calling book.

  • But it's only taking in one string every part of the search for Stone.

  • But my Line three and book has two arguments to the function like What's going on?

  • Yeah, so it's a good question and an easy area for confusion and python, because it is one of the little of strange quirks or new ones is a python.

  • There have been a lot of discussion and debate about whether that's the right way to do things.

  • But Pythons creator has long defended this is the ideal way to do it in the Python programming language, and there are good reasons for it.

  • But the idea here is that this self argument to the innit method is not one that I provided when I created new book.

  • It's just going to be automatically inserted by the Python interpreter.

  • It knows what self is, and that's always going to be the first argument, and I just need to provide the arguments that come after it.

  • All right, Uh, see other question.

  • The cake 1990 was asking, Can you briefly touch on private variables and classes in Python?

  • Yes.

  • So this is a distinction between python and languages like Java, for instance, where in Java you get to have a lot more control over what types of things they're public and what types of things the private notice that there is no notion of like adding public or private to the variables here and pipe on just a bit of a difference between the attributes that we define in python and in Java and so the types of things that you can do with class of end up being a little bit different as a result, Are there some conventions Maybe that we could use to kind of simulate private variables and functions?

  • Yeah, a common convention in Python is if in after Pete starts with an underscore, those attributes should be considered private.

  • And if you're using the cloth, you shouldn't directly try to access those properties that begin with an underscore.

  • You don't technically need to follow that rule from a language perspective.

  • The language doesn't enforce it, but it is a convention of the language.

  • Always is sort of on the programmer, right?

  • Follow the conventions.

  • Pipeline gets the program or a lot of power in that respect.

  • Sham X are no need for self in the objects you twit.

  • Bryan just said Self is implicitly passed in by the interpreter.

  • Book class is returning as book book object, but it classes returning actual integer.

  • Is there a way in a class to return what you want says into already?

  • Five.

  • Uh, can you clarify what you mean by that question?

  • So the B B variable itself right now represents this book or it is a variable inside of which were storing this book object.

  • And that book object is exactly what I've created in this blueprint.

  • It is an object that contains within it a title property whereby I can access the title of the book s so I can say about title to get the title of the book there.

  • Maybe they were confused as to the fact that you could just type in the number 23 it just works like a number.

  • But with book, you know, you're getting this sort of like construction that you've put together with the title and whatnot.

  • It doesn't have, I guess, maybe a more fist behavior, our office polymorphic behavior, depending on the data time.

  • But it may may actually win a case that python special cases like interference stuff so that you can use them as because it's part of the grammar for the language like number.

  • So what do you mean you So you won't be able, Thio.

  • If you try and add a book Thio a number, for instance, that's not going to work like you couldn't you couldn't theoretically define your own class that works like a number because python special cases you might be able to So you can there other properties that you can add it to.

  • This these classes that allow you to get it to behave in particular ways of like comparing whether a thing is less than another, changing the way that it responds to particular operators so that we can get a lot more sophisticated than we've gotten so far in terms of the lexical Parsing, though, like the fact that you can use like 23 as a literal Oh, the 23 literal yeah, that's specifically creates an into object.

  • And that's I imagine that's probably something that I don't know if we could get that kind of flexible, flexible behavior, especially we could lexical e special case in new data.

  • Yeah, as far as I know, there is no way to do that in Python.

  • So if someone knows the way, so I think I think that's probably what they're referring to it.

  • But if I interpreted that incorrectly, please let me know.

  • Let me make sure we didn't miss any other good questions here.

  • And it's the constructor.

  • Yeah, the name of this method is just called a constructor because it's the code that we used to construct a new object in Java.

  • You typically declares his Adam, Auntie, and you typically declare all the attributes that are used in the class.

  • Is this done in python, or is that just done in the constructor?

  • Oh, great question.

  • So in Java, you're probably used to saying, All right, let's define all the properties of the class up front.

  • And so if there's a title for the book and an author for the book, you define all that at the beginning.

  • Not necessarily the case in Python.

  • If I Right now, this book only has a title, and the constructor only set a title.

  • But if I wanted to set the author of this book, I could say Beat Author equals J.

  • K.

  • Rolling, for instance, Sting with Harry Potter thing.

  • That's the same book, so still be so hot.

  • Title is Harry Potter in the Search of Stone and beat off.

  • There is J.

  • K.

  • Rolling.

  • Totally.

  • Something that I can do is well here without needing to have it as a property that's actually defined inside of the book class.

  • Cool.

  • Just make sure we got everything.

  • When did they?

  • Am I late?

  • No, we're still pretty early.

  • Um, what do you guys do?

  • So, Brian, you're an undergrad.

  • Yeah, I'm a technologist.

  • Um, under the hood.

  • Oh, can you, uh, the cake.

  • 19 nineties asking Can you show us how we can define operator overloading in classes?

  • Oh, uh, yeah.

  • Gets a little bit tricky.

  • I want to make sure that we stick with the fundamentals, at least for now.

  • If we have time, we might be able to touch on some of that stuff.

  • Some of the more advanced.

  • And then the last two questions are obligated to determine the type of the property.

  • For example, I need the title to be only a string you.

  • So Python has dynamic typing, meaning we don't enforce the type of the property.

  • So be that title right now is a string.

  • And if I changed the title to be like a floating point number you like, even though it doesn't really make sense to do that, you could do it.

  • There are ways of trying to protect against this.

  • You can add some special code to your classes and say when I try to set the value of her property.

  • You should run this code that maybe add some type checking of your own to make sure that it is in fact, a string.

  • But those air features are available, but not part of the most simplistic way that we could create a class in python.

  • And so one of the things you'll notice about Python as it tries to make the syntax for creating a new class, creating a new object as simple as possible, such that for the vast majority of cases, you can stick with the simplest in tax.

  • But if you want more specific features like the ability Thio validate the types of attributes and the ability to overload operators, For instance, those are things that you can get.

  • But there's require a little more sophistication in the definition of the class.

  • Okay, and then last, they can we override the constructor or, uh, or it's just like the constructor with the default parameter.

  • I guess in initial, can we override the constructor?

  • Yeah, so the general way you do something like that in Python is if I wanted to say allow for me to create a book that I the title.

  • But maybe I also want to be allow a book tohave an author as well, so I can add an author argument to the unit function and then say, like self dot author equals author.

  • And now, if I go ahead and re import from book import book, when I create a new book, Be I'm going to need to say This book is with a Harry Potter and it's by J.

  • K Rolling after specified both arguments to be able to create a book.

  • And if I had only provided one argument, just Harry Potter, well, then I would have gotten a type air because in it was missing one required positional argument.

  • It's missing the author argument, in other words, to create a book I need to provide an author, but I didn't.

  • But some languages, like Java, anything other object during two programming languages will allow you have different constructors, where sometimes you want to be ableto only provide a title and not an author, and sometimes you have both the title and an author, for instance, and so what you could do that Python is by giving these arguments, default values were back inside.

  • Author by default is gonna be none as in has no author.

  • And so now if I say from book in Port Book, let me say Be one is going to be a book called Harry Potter, whose author is J.

  • K rolling on Dhe.

  • Let me move it to this side since I covered up, Um, be one has a title and the one has an author.

  • But if I can also to find a book be too, that is just Harry Potter leaving off the author argument noticed no error this time because if I didn't provide a value for what author is equal to its just going to use that default value of none.

  • So B two's title now is still Harry Potter.

  • But if I try and get a B two's author, there is no But I just always assumed the others J k rolling for every book.

  • Or you could do that.

  • You could give it a default out there J k rolling for everything.

  • Certainly if you want to do all right, I don't want to get us Thio take too long.

  • So if we want, we can maybe forge ahead and we'll come back to look some other questions we would have missed.

  • All right, let's go ahead and keep going, then.

  • In that case, what I'd like to do is I would like to be able Thio, add some code thio this particular book.

  • I'd like the book to be able to do something, and so what I'm going to do is I'm going to add a special method called print info and print Info is going to be a function that I can use on a book that prints out information about the book.

  • Remember that objects and the object oriented programming fence are a combination of data and code data.

  • Has any information associated with the object and the code are going to be these methods, these functions that work on objects that could do something with the object And one thing I might like to do.

  • It's print out information about the book, so I'm going to find a function called print info that takes self again as its argument self is going to be this implicit argument.

  • It's always going to be provided whenever we're calling a method on an object, and that allows me to access the properties of that object.

  • So if I want to print out uh, the book's title, I can print itself title.

  • And then maybe you just concoct Nate It with was written by, We'll Say is written buying his present tense self dot Other.

  • So all the print info function here is going to do is it's going to print out self title, it's going to say is written by and it's going thio self doubt author to the end of that.

  • And so let's go ahead and try that out.

  • Go ahead and reimport books have changed the definition of the book class.

  • I'm gonna go ahead and import the new definition of what the cloth means.

  • Create a new book, Harry Potter, whose author is J K.

  • Rolling.

  • And now I can access the properties individually beat title beat author.

  • But I can also now call this method.

  • I can run this function on the object by saying be dot and in the name of the name of this method is called print info.

  • So say be dot print info, and what I get is okay.

  • Harry Potter is written by J.

  • K.

  • Rolling.

  • Since we since we define that function with self.

  • You know, it's part of that class.

  • When it sees be dot It basically injects be into the function and therefore we have access to all of these information.

  • But you could have done See, you have done D you could have made however many books you want.

  • And they all are gonna print their own sort of self contained message.

  • Yeah, exactly.

  • So if I do find another book, be to, for instance, well defined it as another book that some we'll do.

  • The last lecture, written by Randy Pausch, was a computer science professor at Carnegie Mellon University.

  • I can say B two dot print info and okay, I get an entirely different title, and author be still has its own print info that accessing the properties of the B object.

  • So this is the same function, the same method print info, but depending upon what object I used to call the method, the data inside of it is different, and so the function can behave in different ways.

  • For example, awesome.

  • Somebody somebody ed sorts men said that they like your Web programming course.

  • I'm glad you like it.

  • Yeah, on someone's asking.

  • Oh, It's the same person, actually.

  • And sort of saying, If you could use 3.7 state a class for the data class, I've actually, that's a feature of 3.7 that I have not.

  • Yeah, I haven't really explored that.

  • Maybe potentially.

  • Someone mentioned formatting with F strings.

  • This is a new feature, and python 3.6.

  • Definitely something you could do is well, I'm just using contamination for simplicity.

  • But if you wanted to, you could, uh, talk in F at the beginning of the strange to say, this is gonna be a formatted string and then use curly braces to plug in values.

  • For I could say self dot title is written by self dot author, for instance, and that's gonna have the exact same behavior may actually be a little bit more efficient in this case.

  • Um, Saban, 3 42 asked how to practice learning python.

  • They say they know it but can't solve any problems.

  • How can they increase their problem solving technique?

  • Probably the best way is to practice solving small problems and to see where you get one of my favorites.

  • For this, they vary a little bit of difficulty is Project Oiler Project Oiler.

  • Uh, show you some sample projects.

  • Small tangent.

  • We'll go back to object oriented programming in a moment.

  • Basically, what Project Oiler is is this collection of a lot of problems there.

  • 644 problems.

  • And each of these problems are asking you a question.

  • And usually the way to answer this question is to write some code that can help you to answer it.

  • So click on question one, for instance.

  • It says if we list all the natural numbers below 10 that are multiples of three and five, we get 356 and nine.

  • Okay, those are the numbers less than 10 that are multiples of either three or five.

  • And some of these multiples is 23 on.

  • Then the question is all right.

  • Find the sum of all the multiples of three or five below 1000 and certainly you could do this by hand and figure out what the answer to this is.

  • But what's gonna be easier is all right.

  • See if you can write a python program that gets you the answer to that question, and then you can plug it into the website and see if the answer is correct.

  • So all the problems resemble something like this.

  • They introduce some problems.

  • I ask you a question, and usually the way to solve it is going to be to write some program to be able to do it, and added Brownie points if you can come up with an efficient solution to the problem, trying to figure out how to get it to run very quickly.

  • So good practice for python there many other websites that have sample coding problems as well.

  • But I found the used to be the least interesting problems that often involved with bunch of different types of programs.

  • You have to write a bunch of different ideas.

  • Have you done all 700?

  • I have not done all 700 done.

  • Very few of these actually done.

  • A couple of them, um, beautiful young gentleman asked, Can you avoid having to give default values using a hash or an array as a parameter?

  • Using a hash or no cash table is probably the mean using either hash table or dictionary order Ray, as promoted to a function, can you avoid having to give explicit default values?

  • Uh, in the case of a python function.

  • Your choices are either give the argument a default value or when you call the function to provide a value when you call the function so we don't need a default value on author.

  • But in that case, now you're going to need to tell me who the author is when I try to create a book, then we also be alluding to K are eggs or keyword are?

  • Oh, yes, there are ways where you can specify that you're going to take just some number of optional keyword arguments that could be anything.

  • Usually that's going to happen if you don't necessarily know in advance what those arguments are going to be.

  • But then you can use those arguments in particular ways.

  • You've maybe seen this in use of your taking CS 50.

  • When you are executing database commands, you had TB don't execute and you'd say, like select star from users where username equals user name and we would provide this like username.

  • Argument to d b dot execute We didn't know in advance what all of the possible arguments to the execute function word.

  • So these air just keyword arguments that could be anything.

  • And then the executes functions.

  • Job is to say, All right, let's look at those key word documents, figure out what they are and figure out how to substitute them into the query.

  • Ultimately, that gets parts.

  • Is a dictionary, right?

  • Yeah, that's part of the pipe on dictionary you are.

  • You afforded the ability to pass in a dictionary and have that be your keyword arguments.

  • If you do Starr Starr keyword arguments, can you pass in?

  • The dictionary is that parameter and will consider the same you would have Thio.

  • There's some special thin ducks.

  • Were you at an asterisk before it in orderto sort of exploded out into all the various keyword arguments, so we're sort of getting off topic, but this is good.

  • We just talk about Python in the language, so we'll go ahead and say If the function takes some key word arguments and prints out, we'll just go in and print out the keyword arguments.

  • For now.

  • If I call the function passing in a equals three and like be equals four, for instance, what I get is alright.

  • A dictionary that has a equal to three and vehicle before But if I have a dictionary already called X, where a is said to three and be accepted for if I try to call F with X, that doesn't quite work.

  • But I think if you do f star acts, that means take the dictionary and, like, split its components up into various different parts.

  • Maybe it's star star Star star act.

  • Sorry, you say Star Star ex meaning, Take the dictionary, split it up into all the component parts and then we're able to take the dictionary and basically passage in his keyboard.

  • Arguments I used that.

  • Is this the sort of the pointer syntax?

  • Very rarely.

  • And yeah, it's useful in certain circumstances, though a little bit different in this particular case.

  • We just make sure all caught up in the weekend.

  • We can try to steer course correct back to the object oriented programming.

  • But all this stuff is a lot of fun.

  • I really like going into all kinds of stuff.

  • The Senate protest.

  • What about stars, which we got into and star star for unzipping?

  • Um, we did all that and then remote is asking how convertibles be set to private and you did way mentioned that a little bit earlier.

  • It's a slight difference between Python and Job on that particular sense.

  • Cool.

  • And then this is a good question.

  • Actually.

  • The cake 1990 saying I see object during two programming all over the place.

  • Is this always justified?

  • When does it make sense to stick to object oriented programming compared to traditional imperative or functional programming?

  • Yeah, that's an excellent question.

  • And the choice of programming paradigm really depends upon the type of program that you're trying to write.

  • And there's not necessarily one right answer here.

  • Oftentimes, object oriented programming is a good choice.

  • When you're trying to model Rhea World things that often, it's easy to wait.

  • You have a database.

  • We have a bunch of objects that represent things in the database.

  • Object oriented programming is often a useful way to try and model that data.

  • Someone mentioned the Web programming course, for instance.

  • It's now available on X.

  • If you look at that when we talk about dealing with databases and writing Web programs that interact with databases, we spend some time talking specifically about pythons, object oriented programming, syntax and talking about how we can leverage the features that are given to us, an object going to programming to be able to interact with databases more effectively and in a cleaner way.

  • Imperative programming definitely has its place, too.

  • Certainly for most of CIA fifties problems, that's, for example, they don't necessarily line up quite well.

  • The trying to model objects in the real world.

  • Certainly you could write classes to try and solve them.

  • And so imperative programming makes sense is a style there and then functional programming, which which was brought up well, it's an entirely different paradigm of programming, which is also very interesting, perhaps worthy of a topic.

  • Another time, Yeah, but that has to do more with the treatment of functions is values of their own, where you can begin to take functions and past Emma's arguments to other functions, compose them into higher order functions that you fold over and do various operations on.

  • And there's a lot of very interesting applications of functional programming that gives you access to some nice recursive tool is the ability to write a lot of programs in a very elegant way, and so there's a lot there as well one big sort of thing that people could take about objects in the programming estate, for example, being a thing that's very monolithic and hard to manage for really big applications and functional programming tries to eliminate that right functional programming.

  • In general, a pure function is a function that has no side effects such that no matter what didn't put you provided to the function, it's always going to provide you with the same output, no matter what.

  • And so that could be an advantage there, too.

  • And those were not the only paradigms of programming that exists.

  • There's also lazy programming, lazy evaluation, which is a style of programming wearing.

  • We try and delay computation for as long as possible until we need it compared with functional programming.

  • But it allows you to do things like represent infinite data structures.

  • If I want to represent it, infinite amount of data, imperative programming language could never be able to do something like that because our computer physically can't store an infinite amount of data, but with lazy evaluation.

  • Lacey programming.

  • You can do that by just saying we're gonna store the infinite amount of data, but we're not gonna bother calculating the later pieces of data until we need it.

  • So it offers a lot of interesting tools that you might not have thought possible from just the world of imperative programming.

  • There's declared of programming.

  • And then there's all the whole sort of logic based programming with programming languages like Data Log and Prologue, for instance, there are a number of different styles of programming.

  • Long story short of wigged object oriented programming is just one of them that has particularly uses.

  • When we're trying to model thing from the real world.

  • We have a lot of streams.

  • Future streams we got to do.

  • Okay, cool.

  • Let's get back to the object and programming.

  • That was an awesome assigned.

  • Oh, all right, let's go back to object.

  • Going to programming.

  • What I'd like to do now is maybe defying a new class.

  • We've got a book class, and maybe I wanted to find a class that's going to use this book class, maybe to find a class for what I might want to use this book data somewhere, maybe inside of like, a library, for instance, where we have an inventory of books and we're books could be checked out or returned for instance.

  • So let me go ahead and create a new file called library dot Pie, and I'm gonna create a new class called Library.

  • And I'm gonna define an initial either a constructor function and this constructor function.

  • Right now I'm just going to store like a list of books.

  • What a library has within it is a whole bunch of books.

  • And so we're going to say self dot books.

  • And I'm just gonna by default, Senate equal to the empty list When I first create a library, there are no books that are inside of that library by defaults.

  • We're just gonna make it an empty list again.

  • More of that theme of sort of modeling the real world with integrated programming.

  • Exactly.

  • I'm defining what a library is creating a blueprint for a library where right, By where by.

  • Whenever I create a library, I'm going to initialize it with an empty set of books.

  • And now what I want to do is add some code to this library some operations that I might want to perform on this library, for instance, I might want to add a book to the library at a book just called book, for instance.

  • And what does this add book function?

  • Winter do?

  • Well, it's just going to take self dot books or sorry at book takes self as its first parameter.

  • That's the name of the library object, and it's also going to take the book that I want to add.

  • And I'm just gonna take the self doubt books list, and I'm going to upend to the end of that list.

  • The new book Python's Way of adding to a list of any.

  • Some folks are new to Pine and also shout it to David is in the chat says Thanks for tuning in Everybody, Davis and kind words for um yeah, this is nice, this ability to sort of think a long time Like what?

  • Like I can also envision maybe, like look up book, right?

  • You know, it takes on a string for a title and author or something like, there's a whole bunch of avenues we now have.

  • Yeah, exactly.

  • So we can begin to consider some of the features and people have a future suggestions.

  • Feel free to throw them into the chat.

  • I don't have any particular features we need to implement.

  • So happy to be a little bit creative here.

  • So let me go ahead and import the book class and import the library class on.

  • What I could do is I could also put this important statement at the top of the library cloth, so I only need to do one import.

  • But now what I can say is all right, let me first to find a book called Harry Potter by J.

  • K.

  • Rolling.

  • We'll call that book the one and let me also define B two, which is going to be another book.

  • Will D'oh!

  • My sister's Keeper by Jodi PICO Another excellent book to books and let me know create a library.

  • So I'm gonna say l is going to be equal to a new library and notice that other than self there no other arguments to the innit method inside of this library class.

  • And so I can just say l is going to be equal to a library.

  • So I've created a new library, and if I want to print out the books in the library, I can say l'd up books and I see okay in empty list of books

Hello, world.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

オブジェクト指向プログラミング in PYTHON - CS50 on Twitch, EP.33 (OBJECT-ORIENTED PROGRAMMING IN PYTHON - CS50 on Twitch, EP. 33)

  • 1 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語