Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

5.4 Attributes

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.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
02:08


5.4 Attributes

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.

Back to the top