MODULE-5
1.What is a class? How to define class in python? How to initiate a class and how the class
members are accessed?
class: A programmer-defined type. A class definition creates a new class object
A class definition looks like this:
class Point:
"""Represents a point in 2-D space."""
The class object is like a factory for creating objects. To create a Point, you call Point as if it
were a function.
>>> blank = Point()
>>> blank
<__main__.Point object at 0xb7e9d3ac>
The return value is a reference to a Point object, which we assign to blank.
Creating a new object is called instantiation, and the object is an instance of the class.
>>> blank.x = 3.0
>>> blank.y = 4.0
In this case, though, we are assigning values to named elements of an object. These
elements are called attributes
A state diagram that shows an object and its attributes is called an object diagram.
The variable blank refers to a Point object,
which contains two attributes. Each attribute refers to a floating-point number.
2. What is class, object, attributes. Explain copy.copy() with an example.
class: A programmer-defined type. A class definition creates a new class object
A class definition looks like this:
class Point:
"""Represents a point in 2-D space."""
The class object is like a factory for creating objects. To create a Point, you call Point as if it
were a function.
>>> blank = Point()
>>> blank
<__main__.Point object at 0xb7e9d3ac>
The return value is a reference to a Point object, which we assign to blank.
Creating a new object is called instantiation, and the object is an instance of the class.
>>> blank.x = 3.0
>>> blank.y = 4.0
In this case, though, we are assigning values to named elements of an object. These
elements are called attributes
Copying
box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 0.0
box.corner.y = 0.0
The expression box.corner.x means, “Go to the object box refers to and select the attribute named
corner; then go to that object and select the attribute named x.”
The copy module contains a function called copy that can duplicate any object:
If you use copy.copy to duplicate a Rectangle, you will find that it copies the Rectangle
object but not the embedded Point.
>>> box2 = copy.copy(box)
>>> box2 is box
False
>>> box2.corner is box.corner
True
This operation is called a shallow copy because it copies the object and any references it
contains, but not the embedded objects.
copy module provides a method named deepcopy that copies not only the object but also
the objects it refers to, and the objects they refer to, and so on. You will not be surprised to
learn that this operation is called a deep copy.
>>> box3 = copy.deepcopy(box)
>>> box3 is box
False
>>> box3.corner is box.corner
False
box3 and box are completely separate objects.
3.Define the terms with example: (i) class (ii) objects (iii) instance variables
class: A programmer-defined type. A class definition creates a new class object
A class definition looks like this:
class Point:
"""Represents a point in 2-D space."""
Objects:
The class object is like a factory for creating objects. To create a Point, you call Point as if it
were a function. The variable blank refers to a Point object,
>>> blank = Point()
>>> blank
<__main__.Point object at 0xb7e9d3ac>
The return value is a reference to a Point object, which we assign to blank.
instance variables:
Creating a new object is called instantiation, and the object is an instance of the class.
>>> blank.x = 3.0
>>> blank.y = 4.0
In this case, though, we are assigning values to named elements of an object. These
elements are called attributes
A state diagram that shows an object and its attributes is called an object diagram.
4.Explain the program development concept ‘prototype and patch’ with suitable example.
Chapter -16
1.What is a pure function? Illustrate with an example python program
Or
1.create a Time class with hour, min and sec as attributes. Demonstrate how two Time
objects would be added.
OR
1. Define pure function and modifier. Explain the role of pure functions and modifiers in
application development with suitable python programs.
The function creates a new Time object, initializes its attributes, and returns a reference to the new
object. This is called a pure function because it does not modify any of the objects passed to it as
arguments and it has no effect, like displaying a value or getting user input, other than returning a
value.
OUTPUT :
11:80:00
To get proper time format output we need to modify function above program
Chapter -17
1. Difference between methods and functions.
There are 2 ways of calling method
1.by using class name and by passingobjects
ex:Time.print_time(start)
2. By using object name
Ex:start.print_time()
The reason for this convention is an implicit metaphor:
• The syntax for a function call, print_time(start), suggests that the function is the active
agent. It says something like, “Hey print_time! Here’s an object for you to print.”
• In object-oriented programming, the objects are the active agents. A method invocation
like start.print_time() says “Hey start! Please print yourself.”
2.Explain init and str method with an example python program.
Or
2. Discuss __str__() and __init__() methods used in class definition.
The init method
The init method (short for “initialization”) is a special method that gets invoked when
an object is instantiated. Its full name is __init__ (two underscore characters, followed
by init, and then two more underscores). An init method for the Time class might look
like this:
Output:
The __str__ method
__str__ is a special method, like __init__, that is supposed to return a string
representation of an object.
Output:
3. Explain the concept of operator overloading
When you apply the + operator to Time objects, Python invokes __add__.
Changing the behaviour of an operator so that it works with programmer-defined
types is called operator overloading.
class Time:
"""Represents the time of day."""
def __add__(self, other):
x=self.hour+other.hour
y=self.minute+other.minute
return x,y
And here is how you could use it:
>>> start = Time(9, 15)
>>> duration = Time(1, 35)
>>> print(start + duration)
10:50
3. Define polymorphism. Demonstrate polymorphism with function to find histogram to
count the numbers of times each letters appears in a word and in sentence.
Or
3. discuss polymorphism and demonstrate with and python program.
Functions that work with several types are called polymorphic. Polymorphism can facilitate
code reuse. For example, the built-in function sum, which adds the elements of a sequence,
works as long as the elements of the sequence support addition.
Since Time objects provide an add method, they work with sum:
>>> t1 = Time(7, 43)
>>> t2 = Time(7, 41)
>>> t3 = Time(7, 37)
>>> total = sum([t1, t2, t3])
>>> print(total)
23:01:00
histogram to count the number of times each letter appears in a word.
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] = d[c]+1
return d
This function also works for lists, tuples, and even dictionaries, as long as the
elements of s are hashable, so they can be used as keys in d.
>>> t = ['spam', 'egg', 'spam', 'spam', 'bacon', 'spam']
>>> histogram(t)
{'bacon': 1, 'egg': 1, 'spam': 4}