Loading video player…

Inspecting the Demo Class

00:00 In the previous lesson, you wrote the DemoClass in the demo.py file. Please save this file and then go to your REPL, open it in the same directory where you saved the demo.py file, and then test our code.

00:18 So from demo, import DemoClass

00:23 and also I’m going to import pretty print that just makes it look a bit more readable. So from pprint import pprint.

00:35 So now use pprint() to print DemoClass.__dict__.

00:44 Now before you hit Enter, have a think about what it is you expect to see. Remember, the .__dict__ attribute gives you a namespace in the form of a dictionary.

00:54 So I’m expecting to see all the attributes and methods of my class. So I’m expecting to see in the form of a dictionary, a class attribute, an __init__ method, an instance attribute,

01:07 an instance method, and also a class method. So that’s what we’re expecting. Now, please go ahead and hit Enter, and what do we get? Firstly, we get a mappingproxy.

01:20 A mappingproxy just gives you a read-only view of a dictionary.

01:27 So the dictionary is clearly within the curly braces as always. And what you get is a number of things I was expecting to see. And there’s also something that I was expecting to see that actually isn’t there, but we’ll come back to that in a second.

01:42 So what are the keys and values of this dictionary? There are a number of dunder attributes there. There’s a __dict__, that’s interesting. We are looking at the DemoClass.__dict__ attribute, and that has a key __dict__.

02:00 What is that? Well, let’s park that for a second. We’ll come back to that in a minute. There is the __doc__ key, the value which shows a class’s docstring if there was one, but this class doesn’t have one.

02:12 There is __init__. Ah, we were expecting that. So indeed the __init__ method, that is the __init__ method of the DemoClass and that finds itself at this particular memory address.

02:27 There is __module__. Now __module__, that is a key value of that is demo. And that holds the name of the module in which your class lives.

02:38 Now remember your first line was from a demo, you imported DemoClass. So demo here is the name of the module in which your class lives.

02:49 __weakref__ is not relevant for our purposes. It has to do with garbage collection, I’ll leave it to you to do your own research there. But then we have class_attr, that’s what I was expecting and that is the key of the dictionary here.

03:05 And the value is this is a class attribute. So the key of the dictionary is the name of the attribute, and the value of the dictionary is the value of the attribute.

03:18 We also recognize class_method. So the key in the dictionary is the name of the method. So class_method and the value in the dictionary is the actual method object.

03:32 Now here it is described what that is. It is a class method and it is the DemoClass .class method at a particular memory address.

03:43 We also see then at the bottom the instance_method. And you were expecting that, of course, and that is the .instance_method method of the DemoClass at a particular memory address.

03:58 Now what I’m not seeing is my instance attribute and I don’t quite understand what this __dict__ attribute of the __dict__ attribute is.

04:10 What that is you will cover in the next lesson.

Become a Member to join the conversation.