Module 5 Python
Module 5 Python
8)What is class,object,attributes
Class:
A programmer-defined type is also called a class.
Example:
class enc :
"""studying plc subject."""
Objects
Example, suppose ENC is a class then we can create objects like diva, divb from
the class.
objectName = ClassName()
diva =enc()
divb =enc()
Creating a new object is called instantiation, and the object is an instance of the
class
Attributes
The variables inside a class are called attributes. We use the . dot notation to access
the attributes of a class.
Class point:
x=0
y=0
blank=point()
blank.x=3.0
blank.y=4.0
print(blank.x, blank.y)
Copying
The copy module contains a function called copy that can
duplicate any object.
p1 and p2 contain the same data, but they are not the same
Point:
The is operator indicates that p1 and p2 are not the same object.
box3=copy.deepcopy(box)
box3 is box
False
box3.corner is box.corner
False
A function that does not modify any of the objects it receives as arguments. Most
pure functions are frutitful.
A function is not pure if there is something outside the function that can change its
return, given the same arguments.
Example:
Pure function
11 20 3011 20 30
11:20:30
Modifiers
A function to modify the objects it gets as parameters.
In that case, the changes are visible to the caller. These Functions are called
modifiers.
Sec=65
9:46:5
10)Explain Operator overloading and polymorphism
Example:-1 Example-2
class Point:
self.x = x
x = self.x + other.x
p1 = Point(1)
p2 = Point(2)
print(p1+p2)
p3=Point("hi")
p4=Point(“enc")
print(p3+p4)
Polymorphism
It is a programming term that refers to the use of the same function name, but
with different signatures, for multiple types
Functions that work with several types are called polymorphic. Polymorphism can
facilitate code reuse
This function also works for lists, tuples, and even dictionaries.
Its full name is __init__ (two underscore characters, followed by init, and then two
more underscores)
It is common for the parameters of __init__ to have the same names as the
attributes. The statement self.hour = hour
class Time:
""" represents time in hour,min,sec"""
def __init__(self,hour=0,min=0,sec=0):
self.hour=hour
self.min=min
self.sec=sec
def print_time(time):
print(time.hour,time.min,time.sec)
t=Time()
t.print_time()
0:0:0
x=Time(9)
x.print_time() 9:0:0
y=Time(9,30,45)
y.print_time() 9:30:45
The__str__Method
This method returns the string representation of the object. This method is
called when print() or str() function is invoked on an object.
Example:
def __str__(self):
return '%.2d:%.2d:%.2d' % (self.hour, self.min, self.sec)
t=Time(7) 07:00:00
print(t)
tt=Time(7,56,9) 07:56:09
print(tt)
The built-in function isinstance takes a value and a class object, and returns
True if the value is an instance of the class.
Here we are passing box as an argument and assigns the resulting Point to center
Rectangles
We can ignore the angle and assume that the rectangle is either vertical or
horizontal.
class Rectangle :
"""Represents a rectangle.
attributes: width, height, corner.
"""
To represent a rectangle,
box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 0.0
box.corner.y = 0.0
characteristics:
Programs include class and method definitions.
Most of the computation is expressed in terms of operations on objects.
Objects often represent things in the real world, and methods often
correspond to the ways things in the real world interact.
Time
For example, to change the size of a rectangle without changing its position, we
can modify the values of width and height:
box.width = box.width + 50
box.height = box.height + 100
grow_rectangle takes a Rectangle object rect and two numbers, dwidth and
dheight, and adds the numbers to the width and height of the rectangle:
Output:
>>> box.width, box.height
(150.0, 300.0)
>>> grow_rectangle(box, 50, 100)