Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Counting With a Variable

Tutorial on Underscores and why exporting global variables is a bad idea: https://realpython.com/python-double-underscore/

00:00 In this lesson, you’ll be learning about internal variables. In the previous example, we talked about internal constants and internal functions. In example two, we talk about an internal variable.

00:13 So if you please switch to your favorite code editor,

00:17 this is where we left off after example one. So for example two, we will be adding a new module. So please create another module, file new and call that module count.py.

00:32 And then I’m going to close this again to give us some more space. So you will be creating a counter in this module. The module will have public functions to increment and decrement the counter, and it also has a public function to retrieve the state of the counter.

00:49 Now all of these are part of the module’s public interface, and therefore they can be used directly by the users of your code. But _count, which I will create now, should not be used by your users.

01:04 It starts with an underscore, therefore it is internal and I haven’t used capitals so it is a variable, not a constant. So the first public function of this module is called increment().

01:19 We are using the global variable _count, and the increment() function while it increments the count so _count += 1.

01:33 The second public function is decrement().

01:37 Again, we will be using the global variable _count and decrement() function. I think you can guess what’s coming, -= 1.

01:49 The third public function is the function that gives us the state of the counter, so that is called get_count().

01:59 And what we return is the counter. So we return _count. Of course, you will have spotted that I made an error here. It should say =. So that is our code.

02:15 Why is _count non-public? Users shouldn’t be using _count outside of this module because it can lead to invalid states of the counter.

02:26 So by not directly using _count, your users can avoid those invalid states during the counting process. So to consistently use this module, your users should really modify the current count only with the mutator function, so those public functions increment() and decrement().

02:44 And if they want to know the state of the counter, they should use the public function get_count(). So what does this mean in terms of public and non-public use?

02:55 Well, if we go back to our main here, same as we did before, it is okay to from count import those public functions which were increment(), decrement(), and get_count().

03:14 But what shouldn’t happen is from count, you shouldn’t import _count.

03:23 So as we saw with example one with _PI, Python actually does allow you to import the non-public objects, but that goes against the convention.

03:34 So sticking to the naming convention, it starts with an underscore—you shouldn’t be using it outside of the count module. One final point, in the count module, let’s move back to that for a second, Ww used a global variable. So this variable is global at the module level, you can see we used this global keyword here.

03:58 And exporting global variables is usually not a good idea. So as we saw in the main module here, exporting _count a global variable is not a good idea.

04:10 And why that is exactly the case is a little bit beyond the scope of this video, but I will include a link to a tutorial about single and double leading underscores.

04:20 And there is a good explanation of why exporting global variables is not a good idea.

04:28 Now that you fully understand the use of internal constants and internal functions and internal variables, let’s move on to internal modules in the next lesson.

04:38 See you there.

Become a Member to join the conversation.