How to create a python class

In this tutorial I am going to show you how to create a class in python and then create an instance object from that class. What we want here is to create a python class which will receive the name, job, salary and a boolean parameter which indicates whether that person has changed his or her job recently or not so the default __str__ method from that class will return different string based on that boolean value. OK let get started.

This time I will use eclipse to write the python script but you can use other IDE such as NetBeans 8.1 as well if you want to, both Eclipse and NetBeans are the top IDE I use to write the python script. Before you can create a python project in Eclipse make sure you have installed the PyDev plugin through Help->Install New Software.

Install PyDev
Install PyDev

If you have not yet installed the PyDev plugin before then click on the Add button and provide a name together with this link http://pydev.org/updates to download and install the PyDev plugin.

Once you have installed the plugin in Eclipse you can now go to File->New->PyDev project and create a new python project just like how you create other project in Eclipse.

I will leave you to create the new project with Eclipse and concentrate on the main topic which is how to create a python class.

After creating a package within that project you can now create a new module calls personaldata.py and enter the below script into it.

class PersonalData(object):

        def __init__(self, name, work, earning = 0, change = False):
            self.name = name
            self.work = work
            self.earning = earning
            self.change = change
        
        def __str__(self):
            if(self.change == False):
                return "%s works as a %s and he has a monthly income of %s" % (self.name, self.work, self.earning)
            else:
                return "%s now works as a %s and he has a monthly income of %s" % (self.name, self.work, self.earning)
        
        def changeJob(self, work, income, change = False):
            self.work = work
            self.earning = income
            self.change = change

Every python class needs to have an __init__ method where self is the first parameter which will be automatically passed into the method, self is very important because the instance of that class will use this parameter to call a method or to access a variable of that object.

The rest of the parameters will be passed in at the time you create a new instance of that class. You can also set the default value for each parameter so that instance can use any of those values if any of those values has not been passed into the class at the time you create an instance of that class. The same goes to the class’s method as well.

Now let us create the main python module which will include the PersonalData’s class instance in it. Let create a new python module and name it runmyclass.py. Now you should see those two python modules under the same package.

Folder Tree
Folder Tree

Next enter below script into the runmyclass.py module…

from personaldata import PersonalData

if __name__ == '__main__':
    
    personalData = PersonalData("John", "computer programmer","1000")
    print (personalData)
    personalData.changeJob("project manager", "3000", True)
    print (personalData)

As you can see you will need to import the PersonalData class into this module before you can use it.

If you want to run any python module then you will need this line of statement,

if __name__ == ‘__main__’

make sure you do that.

Next let create an instance of the PersonalData class and enter the second, third and fourth parameter into it, you do not need to enter the fifth parameter if you don’t want to because it already has a default boolean value which is False.

Now you can call the print method and pass in that new instance, what will happen here is this instance will call it’s own __str__ method and return a string based on the boolean value you have passed in, in this example when we first create the new instance Mr. John has not changed his job yet so change = False.  We can overwrite the __str__ method and return any string that we wish to in this program.

We then call the changeJob method and pass three parameters into that method, the last one will be used as the boolean value to decide which string should we return this time.

The program above will produce the below output.

Python Class Demo
Python Class Demo

That is it for this tutorial, as you can see creating class in python is not that hard after all, right? If you are interested in learning more about class in python then continue to read this tutorial Create an inner class in python language.