Skip to main content

Python Class & object

A class is a single object that binds data (instance variable) and functions (class method) together. When we define a class in our source code, we create a new object. Class instances are generally used to access method and data attributes of your class.

Inheritance, encapsulation, operator overload, and other features of object-oriented programming (Oop) are provided via classes. Python classes can define by following syntax which is given in below.

class class_name (Inherit_class_name):
  # statement1
  # statement2
  # statement N

There is possible to define python class in various place. Here given of few examples.

Define a class

# Case 1: define a class 
class Programming:
   def info(self):
obj=Programming() #create object #access function of class
Define A Python Class

In this example, we will create an instance of this class named obj(Object). Information Information is an attribute (class attributes). The Class object has access to this attribute. is executing the class method. And the class instance itself can be called as object which preserves the reference of the class.

Define class in inside function

# Case 2: define class in  inside function
def operation():
  class Inner(object):
    """Function of Inner class"""
    def info(self):
operation() #call function
Python Class Inside Function

In of given example define of python class are within the function. scope of this class is start when execute this function. And scopes is over when executed all statement of this function. so this class scope is dependent of function scopes.

Define class within if statements

#Case 3: define within if statements
  class PythonTest:
    def info(self):
      print("PythonTest info function")
  class CppTest:
    def info(self):
      print("CppTest info function")
  #PythonTest class object    
  #CppTest class object   
else :
  print("Test is Fail Try again") 
PythonTest info function
CppTest info function
python class within if statement

In this example defined class within if statement. So when this statement expression are valid then scopes are start inside class. and observe that both class are same method ("info"). this methode are used by class object.

Basic knowledge of python class

Class attributes: There is two types data attributes and method attributes. data attributes is related to class variables, and method attributes are related to function. For example.

#class attributes example
class Records:
    #setRecord is method attributes 
  def setRecord(self,data): 
    #info is data attributes 
  def getRecord(self):

record=Records(); #create class object
#access method attributes
record.setRecord("Python v3") 
#print data attributes value
print(record.getRecord()) #display value 
Python v3

class instance (objects) : class are combine the method and data. and creating a namespace and scopes. help of class object can be access this namespace attributes. there is defined by using of following syntax.


Let take an example.

#class object example
#Create a class Test
class Test:
    #setRecord is method attributes 
  def setRecord(self,data): 
    #info is data attributes 
  def getRecord(self):

record1=Test(); #create first object
record2=Test(); #create second object

#set object data attribute value
record1.setRecord("Python Test") 

#set object data attribute value
record2.setRecord("Java Test") 

#Dispay data attribute of record1

#Dispay data attribute of record2
Python Test
Java Test
Class Instance

In this example there are two object of class Records. that are binding separate data attributes. that are shared common method but there data attributes are different. And there are no limits to create any number of class instance (objects).

__init__() methods: This is an special methods of class that are automatic invoked when create an class object. this will used to provide an initial value to data attributes. that are similar to constructor of c++ programming. for example

#class __init__ method example
class Test:
  #special method
  def __init__(self,data1,data2): 
    #set initial value
  def display(self):

record1=Test(1,2); #create class object
record2=Test(7,9); #create class object
Class Int method


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment