[Tutor] question about object oriented programming and inheritance using datetime module
Terry Carroll
carroll at tjc.com
Tue Jan 16 20:14:46 CET 2007
On Mon, 15 Jan 2007, Kent Johnson wrote:
> tpc at cryptic.com wrote:
>
> > My class inherits from the date class, but I
> > have to type 'from datetime import date' before I can initialize the
> > class definition. Is there some way to avoid this ?
>
> No, and really there is no reason to want to avoid this. You have to
> import any external module that you want to use directly. Imports are
> very common in Python code and there is no reason not to use them.
I had a similar issue when I started Python, so I think I know what tpc
may be after.
My thought was that I did not want to do the import if the class was not
actually going to be used.
I was really thinking about it the wrong way, though. Really, I would not
want to do the import unless the class was going to be *defined* for use.
The right approach here is to put the class into a module, and the import
statement into the new module as well.
Then, a program that needs to use the class imports the module; and that
module, only if it is imported, imports the classes on which it depends.
In this case, tpc's class might be defined in a MyDateStuff.py module,
which contains:
################
import datetime
class age_calculator(datetime.date):
etc.
################
Then, when he imports MyDateStuff, it imports datetime and defines
age_calculator.
I second Kent's concerns over this, though. It sounds like age_calculator
is really a method that uses datetime.date; not a subclass of
datetime.date.
The question you should ask is: will an age_calculator object actually
also be a date object? If so, subclassing makes sense. Otherwise, think
of using a method.
Put another way, you should think of a statement like
class age_calculator(datetime.date):
as meaning "define a new class named age_calculator; an age_calculator
object is a type of date object."
An example is if you wanted to create a "birthdate" class, which was just
like a regular date, but also included the birthstone that corresponded to
the date. We could create a "birthdate" module that included a
"Birthdate" class:
###############
import datetime
class Birthdate(datetime.date):
def __init__(self, year, month, day):
stones = ["Garnet", "Amethyst", "Aquamarine",
"Diamond", "Emerald", "Perl",
"Ruby", "Python", "Sapphire",
"Opal", "Topaz", "Turquoise"]
self.birthstone = stones[month-1]
###############
We could create a Birthdate object like this:
>>> import birthdate
>>> z = birthdate.Birthdate(1971, 7, 12)
Note, it has the birthstone for July:
>>> z.birthstone
'Ruby'
It also has inherited the other attributes and methods of the standard
datetime.date class on which it was based:
>>> z.isoformat()
'1971-07-12'
Because a birthdate is after all, just a particular kind of date; and the
Birthdate class is just a particular kind of date class.
But in your case, with a name like "age_calculator", it doesn't sound like
an "age_calculator" is a kind of date. It sounds like a thing that
calculates ages.
More information about the Tutor
mailing list