PROGRAMMING

m9/ PYTHON
REMEMBERS




Methods

p288 ! Methods are the same as functions but defined inside the class.
 
class Time: 
    def print(self):
        print('%.2d:%.2d:%.2d' % 
            (self.hour, self.minute, self.second)
        )

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

# Invoking a method is different from calling a function

Time.print(start) # 09:45:00
... 5 lines
 
p291 In functional programming, the function is the active agent: Hey print_time! Here's an object to print In OOP, the objects are the active agents. Hey obj start! Please print yourself
 
class Time:
    
    def int_to_time(self, seconds):
        time = Time()
        minutes, time.seconds = divmod(seconds, 60)
        hour, time.minutes = divmod(minutes, 60)
        time.hour = hour
        return time

    def print(self):
        print('%.2d:%.2d:%.2d' % (self.hour, self.minutes, self.seconds))

start = Time()
start.int_to_time(160).print() # 00:02:40
... 5 lines
 

Questions    
Special Methods

        A B C D E F
🔔
1/1