Python Class & object
Classes are binding of data and function into single unit. When we create a class in our source code that means we are create a new type of object. this object is used to access class properties like such as method and data.
Classes are provide most useful feature of object oriented programming (Oop) like inheritance, encapsulation, operator overload and so on. define class of python by using of following syntax.
class 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): print("Python") obj=Programming() #create object obj.info() #access function of class
In this example Programming is an class. and we are create a instance obj (object) of this class. and info is an function (class attributes). this attribute are access by the class object. in this case obj.info() is execute class function. and self is an reference to calling object.
Define class in inside function
# Case 2: define class in inside function def operation(): class Inner(object): """Function of Inner class""" def info(self): print("information") obj=Inner() obj.info() operation() #call 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 test=1 if(test==1): class PythonTest: def info(self): print("PythonTest info function") class CppTest: def info(self): print("CppTest info function") #PythonTest class object python=PythonTest() #CppTest class object cpp=CppTest() python.info() cpp.info() else : print("Test is Fail Try again")
PythonTest info function CppTest info function
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 self.info=data def getRecord(self): return self.info record=Records(); #create class object #access method attributes record.setRecord("Python v3") #print data attributes value print(record.getRecord()) #display value
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 self.info=data def getRecord(self): return self.info 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 print(record1.getRecord()) #Dispay data attribute of record2 print(record2.getRecord())
Python Test Java Test
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 self.info1=data1 self.info2=data2 def display(self): print(self.info1) print(self.info2) record1=Test(1,2); #create class object record2=Test(7,9); #create class object record1.display(); record2.display();
1 2 7 9