That's part of it. Another is that if in the future you need to add capability to
a class, you can do so within the class without affecting existing software. If you
use just plain functions, you'd have to go to every place where the function was
used and accommodate for the changes. That's called extensability.
Also, a class is like a black box. a lot of what is done inside the class can be
'hidden' at least in a sense from the use of the class.
You can keep all code related to the class as methods that can be called as needed from
the outside.
For example in the code you wrote, you import the class time.
To see all of the methods that are instantaneously available to you,
just from that one import statement, do the following:
- Start a terminal (command) window.
- type
python
- type the following:
>>> import time
>>> help('time')
- explore what's in that one class.
Partial listing from help command above:
Output:
Help on built-in module time:
NAME
time - This module provides various functions to manipulate time values.
DESCRIPTION
There are two standard representations of time. One is the number
of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer
or a floating point number (to represent fractions of seconds).
The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
The actual value can be retrieved by calling gmtime(0).
The other representation is a tuple of 9 integers giving local time.
The tuple items are:
year (including century, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1)
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
CLASSES
builtins.tuple(builtins.object)
struct_time
class struct_time(builtins.tuple)
| The time value as returned by gmtime(), localtime(), and strptime(), and
| accepted by asctime(), mktime() and strftime(). May be considered as a
| sequence of 9 integers.
|
| Note that several fields' values are not the same as those defined by
| the C language standard for struct tm. For example, the value of the
| field tm_year is the actual year, not year - 1900. See individual
| fields' descriptions for details.
|
| Method resolution order: