06.09.2021 Views

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13<br />

Classes and functions<br />

13.1 Time<br />

As another example of a user-defined type, we’ll define a class called Time that<br />

records the time of day. The class definition looks like this:<br />

class Time:<br />

pass<br />

We can create a new Time object and assign attributes for hours, minutes, and<br />

seconds:<br />

time = Time()<br />

time.hours = 11<br />

time.minutes = 59<br />

time.seconds = 30<br />

The state diagram for the Time object looks like this:<br />

time<br />

hours<br />

minutes<br />

seconds<br />

11<br />

59<br />

30<br />

As an exercise, write a function printTime that takes a Time object<br />

as an argument and prints it in the form hours:minutes:seconds.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!