How to create a class in Python

I thought I’d make a post on how you create your very first class by using Python. This is just a beginner guide to get you started.

First off, I will introduce you to the superclass that every class you create in Python inherit methods from: Object. Object have a few built in methods that can be very valuable when you create your own class. I however will just focus on a few in this post, that I have used myself in a small assignment at the university.

To create a class, you start off with simply:

class Classname(): It’s not needed to add Object into the brackets, if nothing else is said the class will inherit Object. If you want you can do it, but it is not necessary.

After this we want to define the __init__() method. It does not return anything, __init__() is just resposible for initializing an instance (the object’s data attributes) of the class when it is created in memory.

Here you can see that I have created a class called Rectangle. Notice the (self) parameter. When a method in the class is executed it must know which object’s data attributes it should operate on. So the self parameter is a reference to the specific object the method is supposed to operate on.Since I want this class to handle input to calculate a rectangle’s area and circumreference the __init__() method also accepts arguments for height and width, which are assigned to the self. __height and self. __width attributes. Two underscores befrore the attribute name makes the attribute private, and can not be manipulated or changed by methods outside the class.

I wanted this class to be able to calculate the area and circumreference of a rectangle I need to define a few methods to make that possible. I’ve choosen to make methods to handle the height, width and also the calculations needed.

Starting off with def getHeight(self): what happens here is that the value stored in the attribute self.__height is returned. The same goes for def getWidth(self):.

In getArea(self): and getCircumreference(self): more happens than in the above methods. Starting off with area, the method returns the sum of the value stored in the attributes self.__height multiplied with the value stored in self.__width.

In getCircumreference(self): we need to have the width multiplied by two and add that to the height multiplied by two. To make sure we get the correct calculation, we have to separate the calculation for 2 times the value of self.__height and the same calculation for self.__width and then add the two values together. Basically just what you could do normally when calculating math.

To see if my class works propperly I make a test program in another file and import the file with the class in it.  You define your main method and then create as many instances of the class you want. I have done two in this test. You add the height and width of the rectangles within the brackets, for example r1=rectangles.Rectangle (8,3). Then to use the methods within the class module you created, you use rectangles you created to call on the methods r1.getCircumreference().

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s