0% found this document useful (0 votes)
5 views7 pages

Unit5 Advanced Python

Uploaded by

jscomputer700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Unit5 Advanced Python

Uploaded by

jscomputer700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit 5

Class : A class is a model for creating objects . class is a user defined data type which is used
to group all the related data and functions. Once we create a class we can create any number of
objects to that particular class
The simplest form of class definition is as follows:
class Class Name:
<statement-1>
.
.
.
<statement-N>
Data Members: the variables declared in a class are data members
Methods: Function declared inside a class are called methods
Data members and methods are collectively called as class members.by defining a class we are
binding data and functions together into a single unit which is known as encapsulation
Object: An object is instance of a class. It is a basic runtime entity.by using objects we can
represent any real time entity.
Constructor;
x = ClassName()
creates a new instance of the class and assigns this object to the local variable x.

The instantiation operation (“calling” a class object) creates an empty object.

Ex:
class Parent:
def pt(p):
print("Parent")
p = Parent()
p.pt()

output:Parent

We created a class Parent and we defined a method pt( ) , here p is the reference by using this
object we call the method pt() which will get excuted.

______________________________________________________________________________

''' Optional information: in python __init__() is constructor


Many classes like to create objects with instances customized to a specific initial state. Therefore
a class may define a special method named __init__(), like this:
def __init__(self):
self.data = [ ]
When a class defines an __init__() method, class instantiation automatically invokes __init__()
for the newly-created class instance. So in this example, a new, initialized instance can be
obtained by:
p = Parent()

'''

https://gvsnarasimha.blogspot.com/ 1
Inheritance: Deriving a new class from an old class is called inheritance. The old class is Base
Class and the new class is Derived class.
The derived class inherits some or all properties of base class. A class
can also inherit properties from more than one class. Inheritance is a concept of reusability i.e.
deriving the properties of one class into another class.
Forms of Inheritance
Single Inheritance
Multilevel Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single Inheritance: A Derived class with only one base class is called single inheritance

In this example we are creating class B by deriving properties of class A


General form of single inheritance is
class DerivedClassName(BaseClassName):
<statement-1>
.
.
.
<statement-N>
Ex:
class Parent:
def pt(p):
print("Parent")
class Child(Parent):
def ch(c):
print("Child")
s = Child()
s.ch()
s.pt()
Output:
Child
Parent
In this example child is the derived class and parent is the base class, we are creating object to
the derived class and we can access the methods of derived class as well as base class

https://gvsnarasimha.blogspot.com/ 2
Multilevel Inheritance:

In multilevel inheritance we derive a class from intermediate base class

Class A serves as base class for derived class B. class B is base class for class C
class Intermediate BaseClassName(BaseClassName):
<statement-1>
.
.
.
<statement-N>
class DerivedClassName(Intermediate BaseClassName):
<statement-1>
.
.
.
<statement-N>

Example Program:
class Parent:#Base Class
def pt(p):
print("Parent")
class Child(Parent):#Intermediate Base class
def ch(c):
print("Child")
class GrandChild(Child):#Derived Class
def gc(g):
print("Grand Child")
g = GrandChild()
g.pt()
g.ch()
g.gc()
output:
Parent
Child
Grand Child

https://gvsnarasimha.blogspot.com/ 3
Multiple Inheritance: A derived class with more than one base class is called as multiple
inheritance

class DerivedClassName(Base1, Base2, Base3):


<statement-1>
.
.
.
<statement-N>
Ex:
class Parent:#Base Class
def pt(p):
print("Parent")

class Child: #Base Class


def ch(c):
print("Child")

#Derived class with multiple Base Classes


class GrandChild(Child,Parent):
def gc(g):
print("Grand Child")
g = GrandChild()
g.gc()
g.ch()
g.pt()
Output:
Grand Child
Child
Parent
Hierarchical Inheritance: When more than one classes are derived from a single base class,
such inheritance is known as Hierarchical Inheritance,

https://gvsnarasimha.blogspot.com/ 4
Example

class Engg():
def first():
print("First Year")

class EEE(Engg):
def sc(s):
print("EEE")
class ECE(Engg):
def ecc(s):
print("ECE")
class CSE(Engg):
def first(s):
print("CSE")
ee=EEE()
ee.sc()
ec = ECE()
ec.ecc()
ee=CSE
cs = CSE()
cs.first()
output:
EEE
ECE
CSE

https://gvsnarasimha.blogspot.com/ 5
Hybrid Inheritance:
Hybrid Inheritance is the combination of any other Inheritance

Example:
class Grandfather:
def gf(s):
print("grandfather")
class Father(Grandfather):
def father(s):
print("father")
class Mother:
def mother(s):
print("mother")
class Child(Father,Mother):
def child(s):
print("Child")
ch = Child()
ch.child()
ch.mother()
ch.father()
ch.gf()
output:
Child
mother
father
grand father

https://gvsnarasimha.blogspot.com/ 6
Regular Expressions:
Regular expression notations are tools for advanced string processing .The re module provides
regular expression capabilities in python. For complex matching and manipulation, regular
expressions offer succinct, optimized solutions:

The most common regular expression notations are

Text - Matches literal


& - Start of string
$ - End of string
(…)* - Zero or more occurrences
(…)+ - One or more occurrences
(…)? - Optional (zero or one)
[chars] - One character from range
[^chars] - One character not from range
Pat | pat - Alternative (one or the other)
(…) - Group
. - Any character except new line
Regular expressions are first compiled into regular expression object
P = re.compile(str)

The operations supported by a regular expression object include:


re.search(text) : Search text for pattern,return match object
re.match(text) : Search anchored at start of string
re.Split(text) : Break occurrences of patterns
re.Findall(text) : returns list of matches of pattern

>>> import re
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'
When only simple capabilities are needed, string methods are preferred because they are easier to read
and debug:
>>> 'tea for too'.replace('too', 'two')
'tea for two'

https://gvsnarasimha.blogspot.com/ 7

You might also like