Exception Handling

Python Class:

Python is a pure Object Oriented Programming language because in Python even data members like “a”  are treated as a object ,while all variables are references of their classes so we can say Python is a Fully Object Oriented Programming Language.Today we will learn class and objects using python .

Class is nothing but a set of attribute .Attributes are data members i.e class variable and instance variable access by “.” dot operator . Class Variable are defined within a class but it can use outside the class by using the concept of inheritance .Class variable and Instance variable hold the data

Create a Class:

class Py:
  def quickdisplay(self):
    print('Hello Python')
  def quickdisplay1(self):
    print('walk to next')

obj = Py()            #create an object
obj.quickdisplay()
obj.quickdisplay1()

 

class output

In above program It shows you how to create a simple class py ,where py is the class name. Here  def is use to define a function or we can say method, print () function is use to print the string value . obj define the object of py class and access by (.) dot operator.

Use Constructor:

Constructor in python is differ from other programming languages like C .In other programming languages constructor name must be same as the class name but in Python Constructor is defined as init().

Syntax:
def _init_(parameter)

How to use Inheritance in Python which is the part of class and object using python

Inheritance is use to inherit the property of a pre-define method of parent/super class, in other language to adopt the properties like child adopt some properties of his father

class Parent:
# method of class  Parent
class child(Parent):
  #use parent method or function
  #methods of child class

for Example:

class Abc:
  attrvalue=100
  def __init__(self):                   #Base class constructor
    print('Parent Constructor')
  def pardis(self):
    print('Pareent Class')
  def setattr1(self , attr):
    Abc.attrvalue=attr
  def getattr1(self):
    print('Parent value :',Abc.attrvalue)
class Bcd(Abc):
  def __init__(self):                                      #child class constructor
    print('Child Constructor')
  def childfunc(self):
    print('Child Function')
v = Abc()                          #instance of Abc class
c = Bcd()                        #instance of Bcd class
c.childfunc()                  #call chilffunc functon
c.pardis()                       #call pardis function
c.setattr1(200)            #call and pass the value in method setattr1
c.getattr1()                   #call getattr1 method

 

inheritance

Overriding Method use in Python:

In overriding we can use the base or parent class method with same name in child class. We can say override.in overriding use in child class to perform different task from parent class

For Example:

class Parent:
  def myfunction(self):                                            #parent class method
    print ('parent method')
class Child(Parent):                                                     #Inhert child
  def myfunction(self):                                            #child class method
    print ('child method')
c = Child()                                    #instance of child class
c.myfunction()                            #child call override method

 

In Python Some Basic Overriding Methods:

1. __init__(self ,arg)

init is constructor with some argument

2. __del__(self)

Destructor ,Delete an object of class

3. __str__(self)

strings represent

4. __cmp__(self)

objects compare

Do you have something to add in class and objects using python? Please add in comments and contribute to the community.

 Follow us on Facebook, Google Plus and Twitter to get more tutorials.

1 COMMENT