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

Creating a Helper Function

00:00 In the previous lesson we did what you can see on the screen now, which is we created a Circle class to show the use of an internal constant _PI.

00:11 In this lesson, we are going to do two things. We’re going to create a second class called Square, and then we going to create an internal function called validate, or _validate that will be used as a helper function in those classes.

00:26 So the first thing to do is to create a second class called Square. And this is going to be a very similar setup. So class Square, remember the capital, the naming convention in Python.

00:40 We create our __init__() method

00:43 and the parameter that is taken by the Square class is called side.

00:49 We then create the attribute by assigning side to .self.side. So that’s our __init__() method. And then you’ll create our method that is also called calculate_area() because it will do the same thing, but of course, it will do this for a square, not for a circle.

01:07 Therefore, the formula is different. You might remember the formula for the area of a square, and that is the side to the power of two.

01:16 I will return the output of that calculation and again, I’m going to round it to two decimals. So .self.side to the power of two. I’m going to round it to two decimals.

01:31 That is our Square class.

01:35 The next thing to do is to create a helper function that will be used by the two classes. So note that I have moved the indentation all the way to the border here. I am not at the same level of def.

01:48 I’m not within the Square class, I’m outside of the class. I’m at that same indentation level and my helper function is going to be called _validate because it is an internal function.

02:03 This helper method will take one input parameter called value. Now what’s the point of this function? When we calculate the area, or when we want to calculate the area of a circle or a square, we take an input, a radius for the circle and a side for the square.

02:19 Now, we automatically assume that these are positive numbers. It’s best to do a check on that to make sure that if the user enters a value that is not usable, that is not a positive number, then an error message should be raised.

02:34 So that’s what this helper function is going to do. So we’re going to say if the input value, so if not isinstance(value, (int, float)), in other words, a usable number,

02:52 or if this value is less or equal to zero, if that is the case, then we would raise an error. Now the first thing I have to do is to make sure there’s a comma here, okay?

03:07 In that case, we will raise an error and the type of error is a ValueError and we’re going to give it a little message saying that we need a positive number, “positive number expected”, right?

03:23 And then we return the value if the error has not been raised.

03:30 So now that we have created the helper function, the point is to use it of course. And where are we going to use it? We’re going to use it in both classes.

03:37 So let’s start with the Circle class. What you want to check is when the radius is being entered that the value that is given to the attribute .self.radius is actually a positive number.

03:51 If that isn’t the case, then you want to raise the error using the validate helper function. So instead of assigning just radius, which could be any kind of thing, it could be characters for all that we know, we’re going to put the validate function around this.

04:06 So we’re going to pass radius into the _validate function. If value is a positive number, then the value is returned, so that’s fine. If it’s not, then an error will be raised.

04:19 So that is what we want.

04:22 So up here in the Circle class, in the __init__() method instead of radius, you put _validate(radius).

04:32 And the same logic applies to the Square class where we have .self.side = side. We’re going to make that _validate( side). Right, so all that’s left to do is to see how this actually works.

04:50 So create a class or a circle called, well circle1 and that is an instance of the Circle class. And we’re going to give it a radius of three and we will print the area into the terminal.

05:05 So print(circle1 and then use the Circles method called .calculate_area())

05:14 and execute that function. And now we’re going to run the Python script. I do that by right-clicking and go run Python terminal.

05:26 And here we have the outcome. It is 28.26. So the code is working, it gives us the area of the circle. Now let’s try and give it a value that doesn’t work, so let’s say I give it a character

05:45 and now I get the error message “positive number expected”. There you go. I leave it to you to test the same thing for the Square class because we do the same thing.

05:57 You would then just instantiate a Square, give it a value for the side. If it’s a positive number, that should give you a number, an outcome for calculate_area().

06:07 If not, it should raise the same positive number, expected ValueError.

06:13 This has been a fairly long lesson already. So let’s take a break. In the next lesson, you’ll pick up from this point and you’ll dig into the public and non-public nature of methods and attributes of your two classes.

06:26 See you there.

Become a Member to join the conversation.