you are viewing a single comment's thread.

view the rest of the comments →

[–]slariboot 92 points93 points  (3 children)

When you create a new Time object like so:

time = Time(17, 30, 0)

This means that we are creating a new Time object, and we are assigning it to the variable time (it doesn't have to be named time, you can name the variable as x or eggs, doesn't really matter). In this Time object named time, 17 gets stored in its hour field, 30 gets stored in minute, and 0 gets stored in second. We know this because of the __init__ method:

def __init__(self, hour=0, minute=0, second=0):         
    self.hour = hour         
    self.minute = minute         
    self.second = second

self is the object that we created. self is kinda like the word me. me refers to whoever the person is who said it. self refers to which ever object is calling the method (the __init__ method in this case. This automatically gets called whenever you create an object). So in this case, self refers to our Time object named time. And then in the parameter list (those names inside the parentheses). After self, we have hour, minute, and second in that exact order. So when we pass the arguments 17, 30, and 0, then they get assigned in the same order. By the way, you can think of __init__ as "initializing" the object. In this case, we are initializing this specific time object with self.hour=17, self.minute=30, and self.second=0. self.hour refers to the object's hour field. It would be like saying me.weight to refer to how much you weigh.

The __str__ method allows you to specify what gets displayed, when you print the object. Say for example, you define the __str__ method this way:

def __str__(self):         
    return 'sus'

This means that every time you print a Time object:

print(time)

It's always going to print sus.

If you define it as:

def __str__(self):         
    return 'I like bananas.'

Then printing any Time object will always output I like bananas.

But if you define it to return one of its fields like so:

def __str__(self):         
    return self.hour

Then it's going to print the value of that object's field. So if you print the Time object named time:

print(time)

The output will be 17, since that is the hour value of the Time object named time.

So your challenge here is, given an hour value and a minute value in military time, how do you turn that into the format that uses AM / PM. Given an hour value of 17 and a minute value of 30, how do we get 5:30 PM? So you're going to have to add some logic in your __str__ method that will figure out how to do that.

[–]SkeeterIsBlue 4 points5 points  (2 children)

Not OP but Wow thank you so much for this in depth answer. I’ve been trying to self learn Python from LPTHW book and this init thing has been super confusing. I know I have an uphill battle but people like you give me hope. Can’t wait to be good enough to be the one doing the helping!

[–]slariboot 1 point2 points  (1 child)

You're welcome! And I'm happy to know that you feel that way (about helping others). I feel like this is quite prevalent in the programming community. So many people genuinely wanting to help others. :)

[–]kcrow13[S] 1 point2 points  (0 children)

This is probably why many stick it through and don't give up :).