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.
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
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;
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)
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.
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
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.