[Tutor] question about object oriented programming and inheritance using datetime module

Kent Johnson kent37 at tds.net
Mon Jan 15 13:05:54 CET 2007


tpc at cryptic.com wrote:
> hey guys, I've been experimenting with Python's datetime module, and I 
> created a function that, given a person's birthdate, calculates how old 
> that person is.  Now I want to create a class called age_calculator that 
> does much the same thing.  

Why? You have a perfectly good function that does what you want, there 
is no need to turn it into a class. One of the strengths of Python is 
that not everything needs to be a class.

> 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.

Inheriting from date isn't a very good idea. You should inherit from 
date if your class will be a specialized kind of date. I guess you could 
think of your class as adding an age() method to date, but you are 
thinking of it as a calculator. Also date objects are immutable which 
makes it harder to create a subclass of date. (You have to override 
__new__() rather than __init__(), but explaining that will probably just 
confuse you at this point.)

There are a few things in your code that are a bit confused - you need 
to call the base class __init__() in your __init__() method, and 
__init__() does not return a value. You should probably read up a bit 
more on classes in Python, either a book or one of the on-line tutorials.

> 
> Also, once I type the import statement and initialize my class 
> definition, I can create an instance of age_calculator.  The instance of 
> age_calculator stores the given birthdate, and gives me a infinite loop 
> traceback when I call self.today().  If I don't inherit from date, I 
> would need to put the import statement somewhere inside a method, and I 
> don't recall ever seeing that done.

It is OK to put an import inside a function or method, but why do you 
think you need to do this?

>  Part of me feels like that's not as 
> elegant as defining an age_calculator class that inherits from 
> datetime.date, but I'm not sure how to do this.  For what it's worth, 
> here's my pseudocode (that inherits from date module) and working code 
> (that does not inherit from date module):
> 
> from datetime import date
> 
> class age_calculator(date):
>     def __init__(self, year, month, day):
>         time_delta = self.today() - self
>         number_of_years = time_delta.days / 365
>         return number_of_years

I'm not sure why this gives an infinite loop (not even sure what an 
"infinite loop traceback" is), but you have not initialized the base 
class so the year, month, day parameters are not used at all.

> 
> class age_calculator:
>     def __init__(self, year, month, day):
>         self.year = year
>         self.month = month
>         self.day = day
> 
>     def calculate_age(self):
>         from datetime import date
>         birth_date = date( self.year, self.month, self.day)
>         date_today = date.today()
>         time_delta = date_today - birth_date
>         number_of_years = time_delta.days / 365
>         return number_of_years

This is better, but compare it to your functional version and you should 
see why the function is preferred.

Kent

> 
> age_calculator(1964, 9, 27).calculate_age()
> 42
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list