Python Class Tutorial with Examples – POFTUT

Python Class Tutorial with Examples


Class is a popular paradigm used in programming language.  Object oriented programming is created the class structures in order to work programming structures like a real world objects. Class and object words are used to express similar things.

Object

Class defines the data types, functions, behaviours but do not holds any data or mostly can not used in a practical manner in python. We should create objects from classes. We can create unlimited objects from class. Class provides only the plan of the object.

Attribute

Attribute may seam a new and original jargon in class but it is very similar to variable. Attributes belongs classes and holds primitive or complex data like integer, string etc.

Use Cases Of Class

Below we have detailed most common usage scenarios of class mechanism in python. Before starting class definition knowing them will give us good hint.

Grouping Data and Functions

We will use classes because we want to group and store data and functions in a single hierarchy and good fashion. We can access data and functions by providing the object attribute which is initialized by class.

Inheritance

Creating classes not enough in big applications. We may need to use them in similar situations without writing from scratch. Classes can be inherited and used without writing all the code.

Define Class

We will use class keyword in order to define class. We will also provide the class name and need to give some class body code to complete class definition. In this example we created a class named student and set a attribute named name which value is empty string.

class student: 
   name=""

We have created a class which seams it is easy like defining function.

Define Attribute Inside Class

We have created a single attribute in order to make class valid and useful. In this part we will create another attributes to make our class more meaningful and functional. In the following example we define new attributes like age and surname 

class student: 
   name="" 
   surname="" 
   age=-1

Define Function Inside Class

Another useful feature of the class is defining functions and using them. There is no difference from normal functions definition. We only put function code into the class code block. In this example we will create a function name printAge which will print student’s age data.

class student: 
   name="" 
   surname="" 
   age=-1 
 
   def printAge(self): 
       print(self.age)

If you realize we have provided self as argument to the printAge function. self is used to provide class attributes into the function to enable usage of class attributes.

LEARN MORE  How To Pass and Parse Linux Bash Script Arguments and Parameters

Initialize Class

In previous parts we have defined the class but do not initialized it. Initialization will create a newobject from class and reserve required memory areas to hold this object’s values. We will initialize the class as object named jack .

class student: 
   name="" 
   surname="" 
   age=-1 
 
   def printAge(self): 
       print(self.age) 
 
jack = student();

This will create a new object jack . We can use this object to access values and functions which is defined by student class.

Access Attribute Inside Class

We can access attributes of a class or value of an object just providing the name of value after object name. We will use point to separate them like below. In this example we will access and get value named age of object jack . We simply used jack.age .

class student: 
   name="" 
   surname="" 
   age=-1 
 
   def printAge(self): 
       print(self.age) 
 
jack = student(); 
 
print(jack.age)

Call Function From Class

Calling functions of objects or class is very similar to getting values. We will provide function name after object name and separating them with point . In this example we will call function named printAge which belongs to jack object.

class student: 
   name="" 
   surname="" 
   age=-1 
 
   def printAge(self): 
       print(self.age) 
 
jack = student(); 
 
jack.printAge()

Set Values At Initialization with __init__

Up to now we have set values in the class definition. This is generally not a practical way to set attributes data. We should set them while creating instance object. We can use __init__ function inside class definition and set attributes by providing into __init__ .

class student: 
 
   def __init__(self,name,surname,age): 
      self.name=name 
      self.surname=surname 
      self.age=age 
 
   def printAge(self): 
       print(self.age) 
 
jack = student("Jack","Cross",19);

As we see __init__ is like a regular function. We have also provided self as first argument which points to our object. Then we get other parameters like name , surname  and age . We simply set them into object with self. and related attribute name. The attributes are automatically created in our object and we can use them like previous attributes definitions.

LEARN MORE  Python Pass Statement

In order to set values in each object we provide related attribute values into class initialization like student("Jack","Cross",19)

Define Empty Class

In some situations we may need to define class but there is no method and attribute where those will be given afterwards. We can use the pass statement which simply means there is no operations in this line but made this line syntactically acceptable. This class will have only name no other usage. In this example the name of the class will be car and provides nothing.

class car:         
   pass

Leave a Comment