Discussing Class Objects
00:00 In this lesson, we are talking about internal classes, so example four. And we’ll be talking about internal attributes and internal methods of classes in example five.
00:13
So if you switch back to your favorite code editor, this is where we left off after the previous example. Let’s create a new module called classes.py
.
00:28
And in this module, we are going to create three classes, two public classes, and one internal or non-public class. So let me make this a bit bigger. The first class is called Class1
.
00:41 It doesn’t start with an underscore so therefore, that is a public class. It doesn’t really matter what it does for now because you’ll find the logic very, very similar to everything we’ve seen before.
00:53 So let’s create a second public class and then let’s create an internal class so it starts with an underscore and capital.
01:03 And what usually I stick in there are helper methods.
01:08
What does this mean? How can you use this? I think you can see what’s coming. We’re going to go to the main.py
file
01:17
and it is perfectly okay from classes
to import the public ones. So those were Class1
and Class2
, but you shouldn’t from classes
import the non-public class from classes
import _Utils
.
01:35 That is not as per the convention. Based on the knowledge that you have built up so far, you can see that the same logic applies. So we’re not going to spend more time on internal classes.
01:51
What we will look at now though is internal attributes and internal methods to classes. So let’s create a new module called class_
example.py
.
02:05
Let me make this a bit bigger again. In here, I’m going to create one class, capital C, and that is ClassA
. Now this class has an internal attribute, so let’s start with underscore, call it _attribute
and give that a value of 10.
02:22 And then we’re going to create a public method
02:28
and don’t forget the self
. And all that method will do is it will print the word “public”. And now let’s create an internal method. So we start with an underscore, and I’m going to call that _helper_method()
and then self
.
02:47 And all this is going to do is print the word “private”. It’s not much of a helper method, but this is just to show how the example works. It’s of course not a real helper method.
02:57
That is our very simple class and let’s play around with that a little bit. Let’s start with if __name__ ==
"__main__":
What are we going to do?
03:08
Well, the first thing to do is create an instance of this class c = ClassA()
. We have created our instance of ClassA
called c
, and that should give us access to its public method.
03:22
So let’s give that a go. .public_method()
, so let’s see what that gives us. Let’s run, run Python file in terminal. And here we go. The word “public” is printed because that is what this method does it prints “public”.
03:41
But now I’d like to show you that you also have access to the internal attribute and the internal method of that class. So if I type c.attribute
03:52
and I am not in the REPL, so I need to add print()
, stick that in the print()
function, then I should see a value of 10 appear because the internal attribute has a value of 10.
04:04 And I get a red squiggly line that’s a warning that this is an internal attribute used outside of the class. We’ll also try and access the internal helper methods.
04:16
So _helper_method()
, and if all goes well, that should then print “private”. Okay, so let’s give that a run.
04:29 And what we indeed see is we see “public”, that is the result of this line of code. We see 10, the result of this line of code, and then we see “private”, which is the result of this line of code.
04:43 And both the internal attribute and the internal method are underlined by this squiggly red line, giving me the warning that I shouldn’t really be using them, but I still can.
04:56
And as a final step, let’s go to the main.py
file
05:03
and here from class_example
,
05:08
it’s perfectly okay to import ClassA
because that is public. class_example
didn’t have any internal classes so I have nothing to add to this section here.
05:21 But if we move back just briefly to the example,
05:25
what I wanted to show is just as constants and variables and functions and classes can be internal to a module, so can class attributes and methods be internal to a class, right? So _helper_method()
is internal to ClassA
.
05:42
So an internal method can be used as a helper method within a class, just like we had _validate()
as a helper function within the shapes.py
module. Just to refresh your memory shapes.py
, we had two classes in there and then we had this _validate()
helper function.
06:03 So the same logic that applies to modules also applies to classes. In the Real Python tutorial, single and double underscores in Python names there are some more detailed examples of classes and how that can be used.
06:18 But for now, that’s the end of this lesson. See you in the next one.
Become a Member to join the conversation.