function - Python Date Class Methods -
brand new here!
this first time working python classes , bit confused. wanted try , store dates 3 attributes - year, month, , day (as integers).
what want is:
for ____init____() function take year, month , date (with defaults 1900, 1 , 1) , return month written out:
>>date_one = date( 1973, 5, 2) may 2, 1973 >>date_two = date( 1984) january 1, 1984
for ____str____() function format data string year, month, , day:
>>date_one = date( 1973, 5, 2) >>string = str(date_one) >>string '1973-05-02' >>date_two = date( 1984) >>print date_two 1984-01-01
for same_date_in_year() determine whether 2 dates fall on same date, if aren't in same year
>>date_one = date( 1972, 3, 27) >>date_two = date( 1998, 4, 17) >>date_three = date(1957, 4, 17) >>date_one.same_date_in_year(date_two) false >>date_two.same_date_in_year(date_three) true
what have far:
days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] month_names = ['', 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'] class date(object): year = 1900 month = 1 day = 1 def __init__(self, year=1900, month=1, day=1): self.year, self.month, self.day = year, month, day print month_names[month], str(day)+', '+str(year) def __str__(self, year=1900, month=1, day=1): self.year, self.month, self.day = year, month, day print str(year)+'-'+str(month).rjust(2,'0')+'-'+str(day).rjust(2,'0') def same_date_in_year(year, month, day): pass if __name__ == "__main__": date_one = date( 1972, 3, 27 ) date_two = date( 1998, 4, 13 ) date_three = date( 1996, 4, 13 ) print "date_one " + str(date_one) print "date_two " + str(date_two) print "date_three " + str(date_three) print "date_one.same_day_in_year(date_two)", date_one.same_day_in_year(date_two) print "date_two.same_day_in_year(date_three)", date_two.same_day_in_year(date_three) print
i have no idea how class work , functions inside it, before pass. if me, appreciated!
as in comment, you're looking init
method, , may able finish rest off on own.
class mdate(object): """example date class learning purposes. note prefix 'm' in class name avoid conflicts built-in classes.""" def __init__(self, year=1900, month=1, day=1): arg in [year, month, day]: if type(arg) != int: raise typeexception('year, month, , day must `int`') self.year = year self.month = month self.day = day
Comments
Post a Comment