REMEMBER PYTHON

MINTE9
2REMEMBER




Init

p293 ! Method __init__ gets invoked when an object is instantiated.
 RunCode
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
... 9 lines
˄˄˄
p294 Method __str__ returns a string representation of an object.
 RunCode
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___
... 11 lines
˄˄˄

Questions    
No. 29   Cond. Expression
CARD
28
32

        A B C D E F
🔔
1/1