PROGRAMMING

m9/ PYTHON
REMEMBERS




Init

p293 ! Method __init__ gets invoked when an object is instantiated.
 
class Time:
    def __init__(self, hour=0, min=0, sec=0):
        self.hour = hour
        self.min = min
        self.sec = sec

    def print(self):
        print('%.2d:%.2d:%.2d' % (self.hour, self.min, self.sec))

time = Time(9, 30) # parameters are optional
time.print() # 09:30:00
... 3 lines
 
p294 Method __str__ returns a string representation of an object.
 
class Time:
    def __init__(self, hour=0, min=0, sec=0):
        self.hour = hour
        self.min = min
        self.sec = sec

    def __str__(self):
        return '%.2d:%.2d:%.2d' % (self.hour, self.min, self.sec)

time = Time(9, 30)
print(time) 
    # 09:30:00
    # print invokes __str___
... 5 lines
 

Questions    
Cond. Expression

        A B C D E F
🔔
1/1