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

Luke Paireepinart rabidpoobear at gmail.com
Mon Jan 15 11:24:39 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.  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.
In order to use class inheritance syntax,
class some_class(some_object):

some_object must be defined.
Otherwise you'll get a variable not defined error.
You could make your own class called 'date' and inherit from that,
but what would be the point of that?
>
> 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.
What makes you think you'd have to import within a method?
Import datetime.date into the global namespace.
That's perfectly alright.
Especially since your class depends on it being available.
>   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):
The way you'd go about doing this is to make an extra function that is 
unique to your inherited class (for example, calculate_age).
>
> 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
This init method is overriding the init of the inherited date class.
The reason today() doesn't work is probably because of this.
>
> 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
>
Just move the import outside of the class.
from datetime import date
class age_calculator:
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day

    def calculate_age(self):
        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

I don't think you really want to inherit from date.
HTH,
-Luke


More information about the Tutor mailing list