Classes and functions in Python

 

Classes and functions in Python

Video Tutorial

To understand the concept we will define a class called Time that records the time of day. The class definition looks like this and We can create a new Time object or instance, also create and assign attributes for hours, minutes, and seconds.

class Time:
    """Represents the time of day.
    attributes: hour, minute, second"""
    
time = Time()
time.hour = 11
time.minute = 59
time.second = 30

print ("Hour: %g" % (time.hour))
print ("Minute: %g" % (time.minute))
print ("Second: %g" % (time.second))

Output

Hour: 11
Minute: 59
Second: 30
Classes and functions in Python - Time Class

Pure functions and modifiers

There are two kinds of functions: pure functions and modifiers.

Pure functions

The function that creates a new Time object, initializes its attributes with new values 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.

To demonstrate this function, I will create two Time objects (instances): the first one is, start contains the start time of a movie which is 9 hours, 45 minutes, and 30 seconds and another is duration contains the run time of the movie, which is 1 hour 35 minutes.

The add_time() function accepts two-time objects, creates a new object sum. Add the two objects start time and duration assign result to sum and finally returns the sum. Here add_time() function is not modifying the passed objects, hence it is called pure function.

class Time:
    """Represents the time of day.
    attributes: hour, minute, second"""
    

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
    return sum

def print_time(t):
    print('Hour:', t.hour, '\nMinute: ', t.minute, '\nSeconds: ', t.second)

start = Time()
start.hour = 9
start.minute = 45
start.second = 30

duration = Time()
duration.hour = 1
duration.minute = 35
duration.second = 0

done = add_time(start, duration)
print_time(done)

Output

Hour: 10 
Minute:  80 
Seconds:  30

The result, 10:80:00 might not be what you were hoping for. The problem is that this function does not handle the cases such as the number of seconds or minutes adds up to more than sixty. When that happens, we have to “carry” the extra seconds into the minute column or the extra minutes into the hour column. The next example handles such issues effectively.

class Time:
    """Represents the time of day.
    attributes: hour, minute, second"""
    

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.minute -= 60
        sum.hour += 1
    
    return sum

def print_time(t):
    print('Hour:', t.hour, '\nMinute: ', t.minute, '\nSeconds: ', t.second)

start = Time()
start.hour = 9
start.minute = 45
start.second = 0

duration = Time()
duration.hour = 1
duration.minute = 35
duration.second = 0

done = add_time(start, duration)
print_time(done)

Output

Hour: 11 
Minute:  20 
Seconds:  0

Modifier Function

Sometimes it is useful for a function to modify the objects or instances it gets as parameters. In that case, the changes are visible to the caller. Functions that work this way are called modifiers. increment() function adds a given number of seconds to a Time object or instance which is visible to the called function.

class Time:
    """Represents the time of day.
    attributes: hour, minute, second""" 

def increment(time, seconds):
    time.second += seconds
    
    m = int (time.second/60)
    time.second = time.second - (m*60)
    time.minute += m
    
    h = int (time.minute/60)
    time.minute = time.minute - (h*60)
    time.hour += h


def print_time(t):
    print('Hour:', t.hour, '\nMinute: ', t.minute, '\nSeconds: ', t.second)

start = Time()
start.hour = 9
start.minute = 45
start.second = 0

seconds = 5000
#print_time(start)
increment(start, seconds)
print_time(start)

Output

Hour: 11 
Minute:  8 
Seconds:  20

The tutorial discusses the concept of Classes and functions in Python, DIfferent type functions such as Pure functions and Modifiers. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds