Create Class and Object in Python

In this tutorial, you will learn how to create class in python with properties and methods, and how to create a new instance of python class and access those properties of that object. This will be very useful to understand python oops concept.

Creating Python Class

Here we learn how to create a student class in python code.
First we create a python file called “student.py”, file name can be different than class name, but here just to keep things simple, we keep the same file name

class Student(object):
    
    def __init__(self):        
        self.Title = "Student";
        self.Name= "";
        print("student class");

Now in above class we have initiated some property value and one print statement

Property Methods in Python Class

In class, at the time of initialization we can declare property in python class, we also can assign some default value or can keep it blank.

Here is an example of properties with and without default values.

     def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname
        self.Mobile;
        self.Address;
        self.Email;
Create a Method in Python Class

Now we will create a method in above student class, we want the method to display a custom welcome message

def welcomeMessage(obj):
   print ("Welcome  " + obj.name)
Code Blocks and Indentation in Python

In most of the programming languages we have seen that indentation is done to make the code looks organised and clean.

But in Python, indentation has a different and very important meaning, indentation indicates logical code block in python.

Notice, in all python code example, space and indentation will indicate the code block, so if you change the indentation, the meaning of the code will also change.

Creating new object in python

We have already seen how to create a class in python and write function, now we will try to access that function by creating a new object of that class

Here in example below we are creating a new instance of student class in a new class called Main, then using that student object accessing some function written in student class.

from School.Student import Student
class Main(object):
   
    student =  Student();  
    student.first_name="Arijit";
    student.last_name="Brahmma";
    
    student.welcomeMessage()

So in above example we have done following three steps

  • Imported the required class using right package and module name
  • Created a new instance (object) of student class inside the Main class
  • Setup property values of new object
  • Then finally executed the function welcome message
Make sure you are accessing the class correctly using right python Package and Module

Access property and methods of python base class

Now we see how to access property and methods of base class, in this example you also learn how to write inheritance in python

In above example we have created a objet of Student class in Main class to access the function welcomeMessage, Now we move the welcome message to base class People and Student class will inherit the method from base class

Notice, Student class is inherited from People class, this is an example of python class inheritence, ChildClass(BaseClass)

from School.People import People 
#this is an example of inheritence
class Student(People):
    
    def __init__(self):
        self.Title = "Student";
    def welcomeMessage(obj):
       print ("Welcome  " + obj.first_name)
       
pass 

Here is the base class definition, all properties are written in base class and a method called personName()

class People(object):
    def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname
        self.Mobile;
        self.Address;
        self.Email;
    def personName(self):
        print(self.first_name, self.last_name)
    

Now in Main class we create a new instance of Student class and access the method of base class.

from School.Student import Student
class Main(object):
    student =  Student();  
    student.first_name="Michael";
    student.last_name="Collins";
    
    student.personName();

Notice, we are calling student.personName(), but personName() method is actually written in base class "People" in above python code.

 
Python Class and Object
Learn python programming with free python coding tutorials.
Other Popular Tutorials
Python programming examples | Join Python Course