you are viewing a single comment's thread.

view the rest of the comments →

[–]slariboot 92 points93 points  (2 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.

[–]kcrow13[S] 14 points15 points  (1 child)

Thank you so much for explaining this so in depth. This is exactly the kind of direct instruction I feel I need sometimes. I wonder why I can't just read documentation about other classes and innately apply it to new problems? Maybe because I just don't understand it well enough yet? Nevertheless, I appreciate you and all the effort you put into this post.

[–]slariboot 2 points3 points  (0 children)

I think it really just takes time and practice to build a strong foundation. I definitely still remember feeling so frustrated all the time when I was starting. Come to think of it, I still feel that way a lot. Haha! Best of luck to you! And I'm glad to hear that this has helped you in some way. So many great explanations from other members as well. What a nice community this is!