Locked lesson.
About this lesson
What are the built-in Python class attributes?
Exercise files
Download this lesson’s related exercise files.
Built-In Class Attributes.docx59.4 KB Built-In Class Attributes - Solution.docx
60 KB
Quick reference
Built-In Class Attributes
Python classes have some built-in attributes that you can access.
When to use
Use these to access Python's built-in class attributes.
Instructions
There are 4 or 5 built-in attributes you can access. To access these, put a dot after your class name and type in the attribute.
Assuming our class is named Employee:
Employee.__dict__ = dictionary, returns class name space information
Employee.__doc__ = returns documentation (you can put a line of documentation in quotes after you define your class name)
Employee.__name__ = returns the name of your class
Employee.__module__ = prints out module info about your class
Employee.__bases__ = prints out base class info in tuple form
Hints & tips
- .__dict__
- .__doc__
- .__name__
- .__module__
- .__bases__
- 00:04 So we've got a pretty good grasp of classes.
- 00:06 We created our Employee class, we initialized it,
- 00:09 created some instance variables.
- 00:11 We created a report method that gives us a little report,
- 00:14 that outputs it to the screen.
- 00:16 We created this fire one that doesn't do anything.
- 00:18 And here we've instantiated, we've created two instances, and we're good to go.
- 00:22 So in this video,
- 00:23 I want to talk about some of the built-in class attributes that Python gives us.
- 00:28 And there's four or five of these attributes.
- 00:30 And basically, they just give us a little bit of information that you may or
- 00:33 may not want to know, but you can access if you do.
- 00:36 To tell you the truth, I don't really ever use these things.
- 00:38 But they're something that you need to learn about classes,
- 00:41 you need to know that they exist.
- 00:42 So there's, like I said, four or five of these things.
- 00:45 And we can access them by just calling our class, Employee,
- 00:50 and then dot, and then calling the attribute.
- 00:53 So the first one is __dict__, and that stands for dictionary.
- 01:01 And that will return a dictionary of your class's namespace, right?
- 01:05 So if we want to print this to the screen, we can do that.
- 01:08 We just print out, like we always have.
- 01:10 If we save this and run it, we get this whole big thing.
- 01:14 It's just a bunch of weird information that we probably don't care about.
- 01:17 But it shows there's a report method function, here's where it is in namespace.
- 01:22 There's a fire method function, okay, so here's where it is in namespace.
- 01:26 There's nothing for the __module__, or the __main__, or the __doc__.
- 01:29 Some of these are other attributes that we're going to look at, these__module__,
- 01:32 __main__, __doc__.
- 01:34 And here's our __init__ method function, and here's where it is in memory.
- 01:38 So again, not super useful, but kind of interesting,
- 01:41 something that you can do with your class.
- 01:44 So that's __dict__, next one is __doc__, and this will return your documentation.
- 01:50 If we save ours and run it, we're not going to get anything,
- 01:52 because we have none.
- 01:53 But I should mention,
- 01:54 you can create a little bit of documentation at the top of your class.
- 01:58 The very first line after you define your class here,
- 02:01 you just wrap this in quotation marks and give it a little, sort of like a comment.
- 02:05 And so we could say, this is my super awesome Employee class, right?
- 02:13 So if we save this, come back down here, and run this again,
- 02:16 it's going to print out that line.
- 02:18 This is my super awesome Employee class, and that's the __doc__,
- 02:21 the documentation for you class.
- 02:23 I don't know why you would really want to ever access that, but if you did,
- 02:26 that's how you would od it.
- 02:27 So that's the second attribute, the third one is __name__,
- 02:32 and guess what this one does?
- 02:34 I bet you can, it just tells you the name of your class.
- 02:38 So that might be actually useful,
- 02:40 you might need to use that in a different program.
- 02:42 You need to call the name, and you can't remember the name, or whatever.
- 02:46 If you need to access the name of your class programmatically,
- 02:49 you can call this attribute.
- 02:50 And likewise, we could,
- 02:54 my_clas_name =, turn it into a variable.
- 02:59 And then later on we could print out, my_class_name, and
- 03:03 getting the same, Employee.
- 03:05 So kind of neat, may be useful, probably, maybe the most useful one of these.
- 03:11 Okay, so __dict__, __doc__, __name__, the next one is __module__.
- 03:19 And this will print out module information about your class.
- 03:21 Of course, we don't have any module, we're just in the main thing of our class, so
- 03:25 not super useful.
- 03:26 The last one is __bases__, and again, this one is not going to be that useful.
- 03:30 But take a look at this, it returns these two parentheses.
- 03:34 And if you think back, that's what a tuple is.
- 03:36 Remember, when we created a tuple, we did it like, my_tuple = (1, 2, 3, 4).
- 03:44 It's these parentheses that make the thing the tuple.
- 03:46 Here, this is returning a tuple, there's no information.
- 03:49 It would return base information in the form of a tuple, if there was some.
- 03:54 Our class is very simple, and we don't have any base information to report, so
- 03:58 there's nothing to see there.
- 04:00 Again, probably, I'm going to say,
- 04:02 __name__ is going to be one of the most useful ones.
- 04:05 You might use that __doc__ one sometime for something or other, and
- 04:08 that's how you would do that.
- 04:10 So for instance, we could call __doc__,
- 04:15 and then we could go, print(employee_1.report()).
- 04:23 And now we get this little header, this is my super awesome Employee class, and
- 04:27 then this thing.
- 04:28 It's kind of a nice little thing, so
- 04:30 you may use that __doc__ built-in attribute in the future.
- 04:33 So those are built-in class attributes.
- 04:35 In the next video, we're going to look at class inheritance.
Lesson notes are only available for subscribers.