10.4 Properties for Data Access

Let’s develop a Time class that stores the time in 24-hour clock format with hours in the range 0–23, and minutes and seconds each in the range 0–59. For this class, we’ll provide properties, which look like data attributes to client-code programmers, but control the manner in which they get and modify an object’s data. This assumes that other programmers follow Python conventions to correctly use objects of your class.

10.4.1 Test-Driving Class Time

Before we look at class Time’s definition, let’s demonstrate its capabilities. First, ensure that you’re in the ch10 folder, then import class Time from timewithproperties.py:

In [1]: from timewithproperties import Time

Creating a Time Object

Next, let’s create ...

Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.