Inspecting the Demo Class Instance
00:00
The previous lesson might have left you scratching your head, thinking, hmm, where is my instance attribute and what is this __dict__
key in that __dict__
dictionary of the DemoClass
?
00:14
To understand what’s happening, please have a look at the class again. On line five, you can see that the instance_attr
is within the __init__
method.
00:26
So what that means is that the instance_attr
is only created once an actual instance is created, because when you create an instance of the DemoClass
, that’s when the __init__
method is being run.
00:37
So the instance attribute only exists if there is an actual instance. Now the instance_method
, on the other hand, on line seven, is not within the __init__
method and therefore the instance_method
you already saw in the .__dict__
attribute of the DemoClass
, but you didn’t see the instance attribute yet because there was no instance.
01:04 The next step, therefore, is to create an instance and see where this instance attribute is.
01:11
Therefore, if you move back to your REPL and create an instance of the DemoClass
, so I’m going to call this demo_instance = DemoClass
with parentheses, and now you have a demo_instance
. Now to inspect the __dict__
attribute just type demo_instance
01:33
.__dict__
And what does that give me? Indeed, the instance_attr
is available here and it says “This is an instance attribute”.
01:46
So now you know where your instance attribute lives. It lives in the .__dict__
attribute of your instance. But what was the dict key up here in your DemoClass
__dict__` attribute?
02:01
Well, this is just a key that maps to the .__dict__
attribute of any instance or object of the class, and that is really what is explained here as well.
02:12
So that should stop you scratching your head. There’s one final point I wanted to cover in this lesson and that has to do with vars()
. Now what is vars()
? vars()
is a function that you can apply to an object and all that vars()
does is actually returns the .__dict__
attribute of that object.
02:34
For example, vars(DemoClass)
02:38
will give you that same mappingproxy
and vars
of the instance. So vars(
demo_instance)
02:49
that will indeed give you the .__dict__
attribute of the instance.
02:55
So that is just another way of viewing the .__dict__
attribute. And the final point on vars()
is that it can be argued that using vars()
for introspection and debugging is actually more Pythonic than using the .__dict__
attribute directly.
03:13 And why is that? Well, dunder methods and attributes are used by Python under the hood. They start with an underscore, as you can see, and are therefore not part of the public interface.
03:26
vars()
, on the other hand, is that public interface, so it would be more Pythonic to use vars()
. Also, according to the Zen of Python, readability counts, and underscores, let alone double underscores, are not very readable, at least not in my opinion.
03:45
With all that said, you are ready to start modifying your objects using the .__dict__
attribute and you will cover that in the next lessons.
Become a Member to join the conversation.