Viewers: in countries Watching now:
Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. In Python 3 Essential Training, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Example projects include a normalized database interface and a complete working CRUD application. Exercise files accompany the course.
Classes in Python are how you make objects. The classes themselves are the blueprint for how an object is created. For example, here is a class called Duck. And it implements a couple of methods called quack and walk, so that when you create a duck object, that duck will quack like a duck and walk like a duck and that of course will make it a duck. An object is an instance of a class. That means that when you create an object, it's a separate thing.
It's separately encapsulated. It has all of its own attributes. So if you make several different objects from a given class, each of those different instance of the class, but they're all separately encapsulated and have their own data space, their own code space and are essentially their own objects. An object is an instance of a class. That means when you create an object, that object is built from the blueprint of the class, but it's its own object. Its own encapsulation. And so if you create several different objects from the same class, they're separately encapsulated.
They have their own data space and they have their own attributes, and they operate independently of one another. So in this case, we've created an object called donald from the Duck class. And we can say donald.quack and donald.walk. And donald will quack like a duck and will walk like a duck and therefore is a duck. Let's take a look at how this works in practice. So we'll start by making a working copy of classes.py, and we'll call it classes-working.py. Go ahead and open that working copy.
And we see we have our Duck class and we have donald. So if we go ahead and run this, you'll see that donald quacks like a duck and donald walks like a duck. So let's take a look at how this works. Class is the keyword that introduces the definition of a class. Duck here is the name of the class and then there is a colon, and so because there is a colon, you have things that are indented under this level. And those are the suite of the class definition.
And in this case we have two things inside the class. We have a function called quack and that function, because it has self as its first argument, is actually a method of the class. And we'll talk in a moment about how this works. Then we have walk, which is defined in the same way, and so that's another method. And then down here, when we create the object donald, we create the object donald by assigning it from Duck.
And Duck is the class definition. And so now donald is an object. And donald is an object of the class Duck. So if I were to just go here and I were to say print(donald), like that, save that and run it, then it says <__main__.Duck object at. And so donald is an object of the class Duck. So when I call the quack method, I use this dot operator and that dot operator is the attribute dereference operator.
That means that it's going to look inside the object donald for an attribute called quack. And because it has these parentheses on it, it's going to go ahead and call it as an object method. And so that will call this code, and it prints this quack, and donald.walk calls this code, and it prints that "Walks like a duck." So Duck is the class and that's all this stuff here, and donald is the object. He's an instance of Duck.
And you can call the methods inside donald. And so you can call the methods inside the donald object of class Duck using this dot notation. So that's how you define a class and create objects based on it in Python. Python is fundamentally an object-oriented language. And that means you're going to do this a lot. There are times when you just have a single thing that you want to do and you want to do it in a very simple way. And for that, you'll just create a function and you'll use a function.
Often you're going to want to do things that are more complicated and these more complicated things are going to have their own local data and they're going to have multiple methods that are tightly related to each other and interact with each other. And these are the times when we're going to go ahead and create a class and use objects. As you go through the process of writing programs in Python, especially if you've not worked with an object- oriented language before, you're going to see more and more cases where objects make sense.
If you're used to using functions a lot, you'll be creating a lot of functions for things, but over time as you get more familiar with the power and the flexibility and the encapsulation and inheritance of the different properties of objects that we're going to go through in this chapter, you're going to use objects more and more. And you'll end up using objects for a lot of the things that you used to use multiple functions for. So objects are very powerful. Python is a fundamentally object- oriented language, so you're going to use these techniques often as you write code in Python.
Find answers to the most frequently asked questions about Python 3 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.