Python Classes and Objects

In This Tutorial or article, you will learn about python classes and objects. Python programming language is an object oriented language.

An object is a collection of variables and functions and a class is a sketch for that objects, In simple words a class explains the content of the objects. It describes data fields that’s called variables and describes the operations or functions that’s called methods.

Check following example to understand, how to create a class in Python programming language

Example   [Create a Class]

 

Output/Results

 

 

 

Create an Object

We use the class name to create an object. Check the following example to understand how to create an object. In this example, you can see the f_Object and f_Class. You can use any name for your object and class.

Example

 

Output/Results

 

 

 

Create a Class including a Method or Function

Check the following example to understand, how to create a function inside the Python classes..

 

Example

 

Output/Results

Self Parameter is an instance of Class that use to access methods of the Class.

 

 

 

Self Parameter

Self Parameter is an instance of Class. We use self parameter to access methods of the Class in Python. Self is just a parameter. so, user can use any name, whatever user want… Check the Examples below to understand the self Parameter.

 

Example#1

 

Output/Results

 

 

Example#2

In This example we will use “sel” and “sef”  in the place of  “self” parameter. And you will see the result will be same…

 

Output/Results

 

 

 

The __init__() Function

The __init__() function is same like a constructor in JAVA programming Language. __init__() Method or function is always executed when the class is initiated. It is most commonly used to provide or assign values to object properties.  Check the given example to understand the __init__() function.

 

Example

 

Output/Results

 

 

Get More Python Tutorials