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): print("Python") obj=Programming() #create object obj.info() #access function of 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. obj.info() 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): 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
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.