Avoiding a Blunder
00:00
I’d like to show you one final thing in this lesson, and this has to do with the non-public nature of _PI
and _validate
. Now, to do this, I’d like you to delete everything below the validate()
function, so leave only all the code up to def _validate
.
00:19
Then I would like you to create a new module called main.py
,
00:28
and then I’ll hide this again. But it’s important to see, this is a different module from shapes
,
00:34 and what you can do is you can import public objects,
00:40 don’t import non-public ones.
00:44
So what that means in this particular case is from the shapes
module, we can import circle
and square
, but we shouldn’t import PI
and validate
. shapes
, import _pi
_validate
.
01:04
It says don’t import non-public objects, but Python doesn’t actually stop you from doing that because despite me saying you shouldn’t import PI
or _validate
, you can actually do that because if I do print(_pi)
, you’ll find that this actually works.
01:23
So if I go run from the terminal, you can see that it brings through the 3.14
value that we gave it in the shapes
module. So this is the point that was made earlier in the course that Python does actually allow you to do it, which is why these are called non-public and not private.
01:42 In other languages, that is not allowed, that is not possible. There are keywords for it, but in Python you can actually do that. But we do rely on the naming convention to not do that.
01:52 So because it starts with an underscore, one or more underscores, you should not import that into your code. That’s it for this lesson. You have just studied the example of a non-public constant and a non-public function, and now let me walk you through the example of a non-public variable in the next lesson.
Become a Member to join the conversation.