0% found this document useful (0 votes)
8 views

IPP M5 C2 Classes - Functions

classes and functions in python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

IPP M5 C2 Classes - Functions

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

Module 4

Chapter 15:
Classes and objects
Chapter 16:
Classes and functions
Chapter 17:
Classes and methods
Classes and Functions
Classes and Functions
• Functions are User Defined Functions
• Consider a Definition of Class
class Time :
pass
• New Objects of
Class the class
can be created
time = Time() time.hours =
11
time.minute = 59
time.search = 30
• User defined Function to
display
the attribute of object
say ob.
Two Kinds of Functions

1. Pure Functions
2. Modifiers
Pure Function

• The function which creates new object ,


initiates its attributes and returns a reference
to the new object is called a pure function.
• It is called pure function because it does not
modify any of the objects passed to it as
arguments of the objects passed to it as
arguments and it has no side effects, such as
displaying value or getting user input.
Example
def add_time(t1, t2):
sum = Time()
sum.hour = t1.hour + t2.hour
sum.minute = t1.minute + t2.minute
sum.second = t1.second +
t2.second

if sum.second >= 60:


sum.second -= 60
sum.minute += 1
if sum.minute >= 60:
sum.min
ute -= 60
Usage of add_time(t1,t2)

>>> start = Time() >>> duration = Time()


>>> start.hour = 9 >>> duration.hour = 1
>>> start.minute = 45 >>> duration.minute = 35
>>> start.second = 0 >>> duration.second = 0

>>> done = add_time(start, duration)


>>> print_time(done)
11:20:00
Modifiers

• The functions which are used to modify


one or more of the objects its gets as
arguments are called modifiers. Most
modifiers are fruitful.
Example
def increment(time, seconds):
time.second += seconds

if time.second >= 60:


time.second -= 60
time.minute += 1
if time.minute >= 60:
time.minute -= 60
time.hour += 1
Prototype and Planned
Development

• Prototype Development
• Planned Development
Prototype Development

• Prototype involves
– Writing a Rough draft of programs ( or prototype)
– Performing a basic calculation
– Testing on a few cases , correcting flaws as we
found them.
• Prototype development leads code
complicated
Planned Development

• High level insight into the problem can make


the programming much easier
• Example : A time object is really a three digit
number in base 60. The second component is
the “Ones Column” the minute component is
the “sixties column” and the hour component
is the thirty six hundred column.
Example

def convertToSeconds(t):
minutes = t.hours*60 + t.minutes
seconds = minutes *60 + t.seconds
return seconds
Example 2

def
makeTime(seconds
): time = Time()
time.hours = seconds//3600
time.minute = (seconds%3600)//60
time.seconds = seconds%60
return time
Example

def addTime(t1,t2):
seconds = convertToseconds(t1) + convertToseconds(t2)
return makeTime(seconds)
EXERCISE
1. Write a function called mul_time that takes a Time object and a number and
returns a new Time object that contains the product of the original Time and
the number.

2. Write a program that takes a birthday as input and prints the user’s age and the
number of days, hours, minutes and seconds until their next birthday.

3. For two people born on different days, there is a day when one is twice as old as
the other. That’s their Double Day. Write a program that takes two birth dates
and computes their Double Day.

4. For a little more challenge, write the more general version that computes the
day when one person is n times older than the other.

You might also like