Loading video player…

Studying an Internal Constant

00:00 In this lesson, you’ll study the use of an internal constant. We are looking at _PI in example number one. You’re going to create a class called Circle.

00:11 And that Circle class will take one parameter, its radius, and it will also have one method to calculate the circle’s area. Now you might remember that to calculate a circle’s area, you need a constant called pi, and it is this constant that we will make an internal constant _PI.

00:32 So that’s your objective. If you please switch to your favorite code editor

00:38 and create a file called shapes.py.

00:42 And then I’m going to hide this to give us a bit more space. And then we create a constant _PI. It’s _PI because it is an internal constant.

00:54 Then create a class called Circle. Circle with a capital that is a naming convention for Python for classes. And then create the .__init__() method. self is what we usually use and then radius,

01:12 and then assign radius to .self.radius.

01:18 So we have our class’s attributes. Now we create a class method called calculate_area().

01:28 And what we will return is, well, the calculated area. So you return the area that the formula for that is in our case _PI, our internal constant, times radius to the power of two.

01:42 But because we are in our class here, we will be using the attributes of .self.radius to the power of two. And I’m going to round this to two decimals just to keep it clean.

01:57 So round to decimals. And that is our class. So the thing to note here is that _PI is an internal constant. It is internal to the shapes.py module.

02:12 It is therefore non-public outside of the shapes.py module, so it’s internal to shapes.py so you can use it freely within this module, but it shouldn’t be used, so it shouldn’t be accessed outside of the shapes.py module.

02:28 The final note I would like to add here is that of course, the point of this example is to show you the use of an internal constant _PI. In the real world if you need to use PI, that is actually available in the math library.

02:43 So you would probably import it from there. So from math import pi, if then instead of _PI, you use pi, you will have a more accurate outcome.

02:56 In the next video, you will be expanding on this example by building an internal function. See you there.

Become a Member to join the conversation.