To code in Python, you'll need to understand how properties are assigned to classes and objects. These properties are referred to as "attributes". This lesson will discuss the basic types of attributes in Python.
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
Okay, so now we have the basic concept of class in our tool belt in an object, and what exactly that is, and how to create one. Now let's start to pick this apart a little bit. So the next thing I want to look at is how and why are we creating these properties basically or these attributes is what they're referred to as within Python. And, if you've ever done any sort of class development in other languages, this may seem a little strange to do it this way, and why is it done within this kind of crazy function, and how do we define them this way with this crazy self parameter. And all those sorts of good things. Well let's pick this apart a little bit. So what exactly is this self? So we're going to kind of break this concept of functions or methods away for a second. We're just gonna analyze this guy right here. So remember I can refer to this as a constructor. So you can think of it that way. So that whenever we are calling our Person class we're creating a new instance object of our Person class like we did previously here. And we're not passing in any data, which seems a little strange because here we have some data. But ultimately Ultimately what's happening here is when we call this what looks to be a function. We are actually calling this constructor function this initialization function here or method to actually set up our class, or set up our object using the defined attributes that are found within the constructor. So let's play around with this a little bit so you can see how do you actually break this and why it doesn't work in certain ways. So let's say, if I didn't put this self in here, because that just seems a little silly, why would I do that? So, let's go ahead and start by doing that. So let's go ahead and save this. Now if I were to come back in here. I'll exit out. I'll clear this out a little bit. And again, I want to say from person I want to import my person class. So now I wanna do the same thing. I want to create a new instance of this person class, which will once again be me. And I wanna be able to get access to these attributes. So I'm gonna say me equal to person, open/close parentheses. And immediately it doesn't work. And the reason it doesn't is once again because Python is pretty good about showing you errors. It says we have a type error here that our constructor takes zero positional arguments. But one was given, but I didn't give it any positional arguments. There's nothing here. Well that's because this is kind of a special case within the world of Python, that when we are creating classes, and you're ultimately when we're working with the methods within this class, or at least specific types of methods, that we'll talk about a little later. We are always getting at least one parameter. Now that one parameter, like I said, can have any name here but that one argument that we're passing in is actually the instance of the class itself. So the way that this works is that you're going to put in the name of this object. And like I said typically the accepted practice is to use the self name. But you can use anything you want. You could have it be purple or sky or grass or whatever, and it will still continue to work. The fact that we've Passed this in here now means that during the call to our initialization method or our constructor we now have access to the actual instance of this class that was passed in here. And then by saying self.first we're now assigning these properties, or what are called attributes within Python, to this particular object. Which is why we're able to refer to these attributes as part of the object or the instance that we're working with. And that's why by defining them this way by attaching them to self or the argument that's being passed into our constructor. We're able to access these attributes as instance attributes. So what exactly does that mean? Well let's save this again and we'll get out of here. And we'll re do this and we'll walk through it a little bit slower. So I'm going to say once again from person, I want to import person. Now I'm gonna say me equal to person. Now, so this time it should work. And it does. And because we've passed in a name for the argument that was being passed in which is our instance of our class. So now, because we've defined these properties or these attributes this way attached to self, like I said these are now. Instance attributes, which means every instance of my Person class. So right now I only have one, I could say me has three defined attributes, first, last and age. And the reason that they're called instance attributes is because they're attached to the instance of my class. So I could say another one, I could say you equal to person. And I can once again say you, first, last and age. So as you see they're going to be attached to all instances of my My class. So why does that matter? Well, there's another type of attribute that you can deal with in the world of Python classes, and that's going to be class attributes.. So a class attribute is slightly different. In a class attribute is really nothing more than an attribute that's defined at the class level and not necessarily at the instance level. So what does something like that look like? Well, In that case we would be doing something that's similar to defining these attributes, but we won't be attaching them to self in the constructor method. And we won't be attaching them to the self instance. And we won't be doing anything with them typically within our constructor method. They're gonna take place outside of that, but still within our class definition here. So I could start to create other attributes at the class level that would typically be associated with all people. It's really hard to make a grave generalization that all people have some particular characteristic, that's a little bit crazy. So I'm just going to throw something else in here by creating a very simple property or a very simple attribute called foo. And we'll just set that equal to the string bar, just to give it a simple example here. So let's go ahead and save this. So now if I were to come back into my interpreter, and once again say, from Person import Person. So now, at this point, if I wanted to create a new instance of my person I could say me = to Person. And as you can see, I've been able to do that still. So, I can say me first. I can do all the normal things, me.last, me.age, and then I can also say Something me.fu. So as you can see here it's going to return bar. So as a member or as instance of a particular class, I do get access to these class level attributes. Just the same way as if they were attached to me, but the difference here is also that now I can say things like person.foo. Now at the class level I'm also able to get access to that. So like I said, typically the times when you would use class level attributes would have to do when you are defining some sort of characteristic that is very specific to all people. So maybe a better example would be if I were to defining a class say of car. And maybe you could have a bunch of different instance attributes that would define things like the make, and the model, and the year that it was built, and all sorts of things like that. And maybe you would have a class attribute of say the number of wheels equal to four, because one of the defining characteristics of a car is that it has four wheels. So as you can start to see, we can start to break things out that way where you have these attributes that are very specific and very mutable based on that the different instances that you're creating for a particular Class. And then you also have the concept of creating these properties or these attributes at the class level that are basically going to be the same for the most part across all different instances of that particular class. So the opposite here of course is not necessarily true where I get access to this class level attribute from both just the class as well as the instance. The inverse is not the same, so I can't say something like Person.first, I'm going to get an error here because Person has no attribute first, it doesn't know at the class level what first is, that is only accessible at the instance level. So that's why we've defined those here within our constructor or in this our Initialization method. So those are some of the basic concepts behind the attributes. We start to talk about things like instance attributes and class attributes. And on the next lesson we're gonna dip a little bit into the world of these methods a little bit more. And we're gonna talk about some of these similar concepts to these instance and class attributes. And how methods are kind of viewed in the same way.